希望有人可以帮我解决这个问题。此代码将税收作为我的同位素过滤器/物品的类输出。它在localhost上工作正常,但一旦上传它仍然有效,但会产生以下错误:
*警告:在第#行的/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; ?>
答案 0 :(得分:1)
*警告:在第#*
行的/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; ?>
答案 1 :(得分:0)
我认为问题是你的$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; ?>