1
Warning: array_shift() expects parameter 1 to be array, object given in /hermes/walnaweb01a/b1374/moo.peachdesigninccom/tools4hardwood/wp-content/themes/listings/homepage_loops/content-listingsum.php on line 18

Is an error I'm getting on this page http://www.tools4hardwoods.com/home-2/ and I'm having no luck. Can some one help me debug?

Heres the full code:

<div class="car-list">
    <div class="car-img">
        <?php if(has_post_thumbnail()): ?>
            <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail("home_listing"); ?></a>
        <?php endif; ?>
    </div>
    <div class="car-info">
        <a href="<?php the_permalink(); ?>"><h3><?php the_title(); ?></h3></a>
        <h2 class="car-price"><?php get_listing_price(); ?></h2>
        <ul class="car-tags clear">
            <?php
            global $post;
            $configuration = get_listing_display_attributes($post->ID);
            if ($configuration):
                foreach ($configuration as $tax) {
                    $terms = get_the_terms($post->ID,$tax);
                    if ($terms):
                        $term = array_shift($terms);
                        $term_link = get_term_link($term->slug,$term->taxonomy);
                        $term_name = $term->name;
                        $taxonomy = get_taxonomy($term->taxonomy);
                        ?>
                        <a href="<?php echo $term_link; ?>"><?php echo $term_name; ?></a>
                    <?php
                    endif;
                }
            endif;
            ?>
        </ul>
    </div>
    <div style="clear:both;"></div>
</div>
3
  • What do you get if you print_r($terms)? It's not an array here. WP says it can return false. Commented Nov 10, 2016 at 21:25
  • $79 for a dewalt hand sander? Commented Nov 10, 2016 at 21:27
  • Could you put your suggestion inside of the full code or at least let me know which line of code to replace it with? Commented Nov 10, 2016 at 22:29

2 Answers 2

1

The get_the_terms() function can return more than just an array or false. It can also return a WP_Error object.

By doing if ($terms) you are only checking for it to be truthy, which an object is.

Instead you should do this:

if (is_array($terms)) {
   // Do something
} elseif ($terms instanceof WP_Error) {
   // Handle error
}
Sign up to request clarification or add additional context in comments.

1 Comment

Could you put your suggestion inside of the full code or at least let me know which line of code to replace it with?
0

As pointed out by @Jeremy, this part of your code may return WP_Error instance:

// May return array, false or WP_Error object.
$terms = get_the_terms($post->ID,$tax);

So you have to update your conditional, make sure that the $terms is an array and not WP_Error object.

<?php

global $post;
$configuration = get_listing_display_attributes($post->ID);
if ($configuration):
    foreach ($configuration as $tax) {
        $terms = get_the_terms($post->ID,$tax);
        // Update your conditional here.
        if (is_array($terms) && ! is_wp_error($terms)):
            $term = array_shift($terms);
            $term_link = get_term_link($term->slug,$term->taxonomy);
            $term_name = $term->name;
            $taxonomy = get_taxonomy($term->taxonomy); ?>
            <a href="<?php echo $term_link; ?>"><?php echo $term_name; ?></a>
        <?php endif;
    }
endif;
?>

The is_wp_error() is a built-in Wordpress method to check whether the passed parameter is an instance of WP_Error class.

Hope this help!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.