希望有人能帮助我这个代码。此代码将同位素过滤器/items的税作为类输出。它在本地主机上工作得很好,但一旦上传它仍然工作,但会产生以下错误:
*警告:为/path-to-file.php中的foreach()提供了无效的参数*
<?php while ( $the_query->have_posts() ) : $the_query->the_post();
$termsArray = get_the_terms( $post->ID, "print_type" ); //Get terms for item
$termsString = ""; //initialize string that will contain the terms
foreach ( $termsArray as $term ) { // for each term
$termsString .= $term->slug.' '; //create a string that has all the slugs
}
?>
<div class="<?php echo $termsString; ?>">
</div>
<?php endwhile; ?>
*警告:为/path-to-file.php中的foreach()提供了无效的参数# *
当您为foreach循环提供的数据既不是数组也不是对象时,会给出此警告。只要添加一个if条件来检查是否相同就可以了。
<?php
while ( $the_query->have_posts() ):
$the_query->the_post();
$termsArray = get_the_terms( get_the_ID(), "print_type" ); //Get terms for item
$termsString = ""; //initialize string that will contain the terms
// Only use foreach for a array or object.
if( is_array($termsArray) ){
foreach ( $termsArray as $term ) { // for each term
$termsString .= $term->slug.' '; //create a string that has all the slugs
}
}
?>
<div class="<?php echo $termsString; ?>">
</div>
<?php endwhile; ?>
我想问题出在你的$post->ID
,它没有红利。试试这个
<?php
$guide = array(
'post_type' => 'post', //type post type name here
'posts_per_page' => -1, //number of posts
);
query_posts($guide); while(have_posts()) : the_post(); ?>
<?php
$cats = get_the_terms(get_the_ID(),'type_terms_name_here');
if($cats){
foreach ($cats as $value){
echo $value->term_id; //call term id
echo $value->name; //call term name
echo $value->slug; //call term slug
echo $value->term_group; //call term_group
echo $value->term_taxonomy_id; //call term_taxonomy_id
echo $value->taxonomy; //call term taxonomy type
echo $value->description; //call term description
echo $value->count; // call term post count
}
}
?>
<?php endwhile; ?>