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
我在这个网页http://www.tools4hardwoods.com/home-2/上遇到的错误,我没有运气。有人可以帮我调试吗?
下面是完整的代码:
<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>
答案 0 :(得分:1)
get_the_terms()函数不仅可返回数组,还可返回false。它还可以返回WP_Error个对象。
通过执行if ($terms)
,您只是检查它是否真实,对象是。
相反,你应该这样做:
if (is_array($terms)) {
// Do something
} elseif ($terms instanceof WP_Error) {
// Handle error
}
答案 1 :(得分:0)
正如@Jeremy指出的那样,这段代码可能会返回WP_Error
个实例:
// May return array, false or WP_Error object.
$terms = get_the_terms($post->ID,$tax);
因此您必须更新条件,确保$terms
是一个数组,而不是WP_Error
对象。
<?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;
?>
is_wp_error()
是一个内置的Wordpress方法,用于检查传递的参数是否为WP_Error
类的实例。
希望这有帮助!