skip to Main Content

I am new to php. I have these errors appearing on some WordPress pages.

Warning: count(): Parameter must be an array or an object that implements Countable in /www/tastingvictoria_289/public/wp-content/themes/astra-child/template-parts/content-single.php on line 38

Warning: Invalid argument supplied for foreach() in /www/tastingvictoria_289/public/wp-content/themes/astra-child/template-parts/content-single.php on line 40

This is the related code.

<?php $terms = get_the_terms( $post->ID , 'category' ); 
        $total = count($terms); // 38
        $i=0;
        foreach ( $terms as $term ) {
            if($term->slug != "featured-post"){
                $i++;
                $term_link = get_term_link( $term, 'category' );
                if( is_wp_error( $term_link ) )
                continue;
                echo '<p class="category"><span><a class="" href="' . $term_link . '">' . $term->name . '</a></span></p>';
                if ($i != $total) echo ' ';
            }
            
        } 
        ?>

Any explanation?

4

Answers


  1. As the error message, the $term parameter that you pass into count() function is not countable (in some case – eg, the post id is not exit).

    To fix this, please change your code into:

    <?php $terms = get_the_terms( $post->ID , 'category' );
    if(is_array($terms)){
        $total = count($terms); // 38
        $i=0;
        foreach ( $terms as $term ) {
            if($term->slug != "featured-post"){
                $i++;
                $term_link = get_term_link( $term, 'category' );
                if( is_wp_error( $term_link ) )
                continue;
                echo '<p class="category"><span><a class="" href="' . $term_link . '">' . $term->name . '</a></span></p>';
                if ($i != $total) echo ' ';
            }
            
        }
      }
    ?>
    
    Login or Signup to reply.
  2. Convert the $terms to an array for getting valid result of count($terms).

    Login or Signup to reply.
  3. get_the_terms() returns Array of WP_Term objects on success, false if there are no terms or the post does not exist, WP_Error on failure.

    May be the terms not exists. Try following:

    $terms = get_the_terms( $post->ID , 'category' );
    
    if ( $terms && ! is_wp_error( $terms ) ) : 
    $total = count($terms);
    $i=0;
    foreach ( $terms as $term ) {
        if($term->slug != "featured-post"){
            $i++;
            $term_link = get_term_link( $term, 'category' );
            if( is_wp_error( $term_link ) )
            continue;
            echo '<p class="category"><span><a class="" href="' . $term_link . '">' . $term->name . '</a></span></p>';
            if ($i != $total) echo ' ';
        }
        
    }
    endif;
    
    Login or Signup to reply.
  4. Mainly the get_the_terms() function behind code like this

    function get_the_terms( $post, $taxonomy ) {
        $post = get_post( $post );
        if ( ! $post ) {
            return false;
        }
     
        $terms = get_object_term_cache( $post->ID, $taxonomy );
        if ( false === $terms ) {
            $terms = wp_get_object_terms( $post->ID, $taxonomy );
            if ( ! is_wp_error( $terms ) ) {
                $term_ids = wp_list_pluck( $terms, 'term_id' );
                wp_cache_add( $post->ID, $term_ids, $taxonomy . '_relationships' );
            }
        }
     
        /**
         * Filters the list of terms attached to the given post.
         *
         * @since 3.1.0
         *
         * @param WP_Term[]|WP_Error $terms    Array of attached terms, or WP_Error on failure.
         * @param int                $post_id  Post ID.
         * @param string             $taxonomy Name of the taxonomy.
         */
        $terms = apply_filters( 'get_the_terms', $terms, $post->ID, $taxonomy );
     
        if ( empty( $terms ) ) {
            return false;
        }
     
        return $terms;
    }
    

    In this case, you can see if there is no post then it’ll return false, and if the function returns false then your code will be like this:

    <?php $terms = get_the_terms( $post->ID , 'category' ); 
            $total = count($terms = false); // if there is no post
            $i=0;
            foreach ( false as $term ) { //if there is no post
                if($term->slug != "featured-post"){
                    $i++;
                    $term_link = get_term_link( $term, 'category' );
                    if( is_wp_error( $term_link ) )
                    continue;
                    echo '<p class="category"><span><a class="" href="' . $term_link . '">' . $term->name . '</a></span></p>';
                    if ($i != $total) echo ' ';
                }
                
            } 
       ?>
    

    Note: the count() function accept an array or object and the foreach() function accept iterable_expression as well. That is why you’re getting the warnings.

    So, in that case, you can check the return output of the get_the_terms() function like this:

    <?php $terms = get_the_terms( $post->ID , 'category' );
    if(is_iterable($terms)){
        $total = count($terms); // 38
        $i=0;
        foreach ( $terms as $term ) {
            if($term->slug != "featured-post"){
                $i++;
                $term_link = get_term_link( $term, 'category' );
                if( is_wp_error( $term_link ) )
                continue;
                echo '<p class="category"><span><a class="" href="' . $term_link . '">' . $term->name . '</a></span></p>';
                if ($i != $total) echo ' ';
            }
            
            }
          }else{
            //do something
          }
        ?>
    

    Thank you

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search