PHP警告:array_shift()期望参数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
是我在这页上得到的错误http://www.tools4hardwoods.com/home-2/,我没有运气。有人能帮我调试吗?PHP警告:array_shift()期望参数1是数组
继承人的全码:
<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>
正如@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
类的一个实例。
希望得到这个帮助!
啊!这是一个巨大的帮助!这工作!谢谢! – kgmack
的get_the_terms()函数可以返回不仅仅是一个阵列或假多。它也可以返回一个WP_Error对象。
通过做if ($terms)
你只是检查它是truthy,其中一个对象是。
相反,你应该这样做:
if (is_array($terms)) {
// Do something
} elseif ($terms instanceof WP_Error) {
// Handle error
}
你可以把你的建议放在完整的代码中,或者至少让我知道哪一行代码替换它? – kgmack
如果你使用'print_r($ terms)',你会得到什么?这不是一个数组。 WP说它可能会返回错误。 – Popnoodles
dewalt手摇砂光机79美元? –
你可以把你的建议放在完整的代码中,或者至少让我知道哪一行代码替换它? – kgmack