基本上,我想检查某个帖子是否属于特定类别,但就我的一生而言,我做错了什么。这是当前代码
$post_catz = wp_get_post_categories( $this->post->ID );
$catz = array();
foreach($post_catz as $c){
$cat = get_category( $c );
$catz[] = array( 'name' => $cat->name, 'slug' => $cat->slug );
if ($catz['slug'] = 'featured') {
$colorslist = 'colorlistingfeat';
}
}
$colorslist
是我要更改的变量,如果帖子来自该特定类别。
答案 0 :(得分:0)
if ($catz['slug'] = 'featured') { $colorslist = 'colorlistingfeat'; }
对于比较运算符,您必须使用==
而不是=
http://php.net/manual/en/language.operators.comparison.php
希望获得帮助。
答案 1 :(得分:0)
您的代码在检查数组时有错误。试试这个吧。
<?php
$post_catz = wp_get_post_categories(get_the_ID());
$catz = array();
foreach($post_catz as $c){
$cat = get_category( $c );
$catz[] = array( 'name' => $cat->name, 'slug' => $cat->slug );
}
foreach($catz as $cs){
if($cs['slug'] == 'featured') {
$colorslist = 'colorlistingfeat';
}
}
echo $colorslist;
?>
但是请确保$this->post->ID
返回帖子ID。
答案 2 :(得分:0)
我相信这是因为您的代码$catz[] = array( 'name' => $cat->name, 'slug' => $cat->slug );
。请注意,$catz[]
是一个数组变量。因此,您应该以{{1}}的形式获取该标签,否则$catz[0]['slug]
只会返回$catz
的值。将代码编辑为Null
还要注意您的if语句。应该是if(cond == cond)