kgmack
kgmack

Reputation: 99

PHP Warning: Warning: array_shift() expects parameter 1 to be array

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>

Upvotes: 1

Views: 2057

Answers (2)

Risan Bagja Pradana
Risan Bagja Pradana

Reputation: 4674

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!

Upvotes: 0

Jeremy Harris
Jeremy Harris

Reputation: 24579

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
}

Upvotes: 1

Related Questions