• Resolved Murtaza Frosh

    (@maimooninc)


    I get

    Warning: array_shift() expects parameter 1 to be array, boolean given in C:\wamp\www\Dropbox\ktechwp\wp-content\themes\ktech\single-shredders.php on line 118

    for a loop that goes to the taxonomy ‘shredder-usage’ under custom post type ‘shredders’ and shows all the post under that taxonomy term. The following code works in the single-safes.php

    <div id="othrsizes"><h2>Other Products</h2>
    
    <?php
    
    $sectors = get_the_terms( get_the_ID(), 'safe-type' );
    
    if ( ! is_wp_error( $sectors ) ) {
    
    	$term = array_shift( $sectors );
    
    	/* Query for jobs that are in the sector. */
    
    	$jobs = null;
    
    	if ( isset( $term->slug ) && isset( $term->taxonomy ) ) {
    
    		$jobs = get_posts( array(
    
    			'term'        => $term->slug,
    
    			'taxonomy'    => $term->taxonomy,
    
    			'post_type'   => 'safes',
    
    			'post_status' => 'publish',
    
    			) );
    
    	}
    
    	/* Loop over all jobs and display an unordered list. */
    
    	if ( $jobs ) {
    
    		$_post = $post;
    
    		print '<ul>';
    
    		foreach ( (array) $jobs as $post ) {
    
    			setup_postdata( $post );
    			echo '<div class="productimg"><a href="' . esc_url( get_permalink() ) . '">';
    			the_post_thumbnail();
    			the_title( '<li><a href="' . esc_url( get_permalink() ) . '">', '</a></li>' );
    
    			if(get_field('new_price'))
    			{
    				echo '<span class="old_price">Rs. ' .get_field('price') . '</span> > <span class="price">Rs. ' . get_field('new_price') . '</span>';
    			}
    			else
    			{
    				echo '<span class="price">Rs. ' . get_field('price') . '</span>';
    			}
    
    			echo "</a></div>";
    
    		}
    
    		print '</ul>';
    
    		$post = $_post;
    
    	}
    
    }
    
    ?>
    
        </div>

    but the following code which is the same after I change the taxanomy name ‘safes’ top ‘shredder-usage’ and post type ‘safes’ to ‘shredders’ does not work and gives me warning mentioned above.

    <div id="othrsizes"><h2>Other Products</h2>
    
    <?php
    
    $sectors = get_the_terms( get_the_ID(), 'shredder-usage' );
    
    if ( ! is_wp_error( $sectors ) ) {
    
    	$term = array_shift( $sectors );
    
    	/* Query for jobs that are in the sector. */
    
    	$jobs = null;
    
    	if ( isset( $term->slug ) && isset( $term->taxonomy ) ) {
    
    		$jobs = get_posts( array(
    
    			'term'        => $term->slug,
    
    			'taxonomy'    => $term->taxonomy,
    
    			'post_type'   => 'shredders',
    
    			'post_status' => 'publish',
    
    			) );
    
    	}
    
    	/* Loop over all jobs and display an unordered list. */
    
    	if ( $jobs ) {
    
    		$_post = $post;
    
    		print '<ul>';
    
    		foreach ( (array) $jobs as $post ) {
    
    			setup_postdata( $post );
    			echo '<div class="productimg"><a href="' . esc_url( get_permalink() ) . '">';
    			the_post_thumbnail();
    			the_title( '<li><a href="' . esc_url( get_permalink() ) . '">', '</a></li>' );
    
    			echo "</a></div>";
    
    		}
    
    		print '</ul>';
    
    		$post = $_post;
    
    	}
    
    }
    
    ?>
    
        </div>

    Please review and help

Viewing 2 replies - 1 through 2 (of 2 total)
  • popper

    (@julialasarte)

    get_the_terms will return false if no terms are found in the given taxonomy. WordPress is not finding any terms in ‘shredder-usage’, returning false and causing that error, since you are passing a boolean to array_shift.

    Thread Starter Murtaza Frosh

    (@maimooninc)

    hello Mr. Popper;

    Thank you for your feedback. Actually you are right the terms were not published properly in WP so I re-published them and it worked. Thanks for your inputs.

    Regards
    Murtaza Frosh
    Maimoon Inc.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Warning: array_shift() expects parameter 1 to be array error in WP post loop’ is closed to new replies.