Basically, I want to check a post if it's in a specific category but for the life of me, I'm doing something wrong. Here's the current code
$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
is a variable that I want to change, if the post is from that specific category.
Your code having a bug in checking the array. Try this instead.
<?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;
?>
But ensure that $this->post->ID
returning post ID.
if ($catz['slug'] = 'featured') { $colorslist = 'colorlistingfeat'; }
For comparison operators you have to use ==
not =
http://php.net/manual/en/language.operators.comparison.php
Hope this help.
I believe this is because your code $catz[] = array( 'name' => $cat->name, 'slug' => $cat->slug );
. Notice $catz[]
is an array variable.. so you should get the slug as $catz[0]['slug]
or else the $catz
will just return Null
value. Much better to edit your code as $catz = array( 'name' => $cat->name, 'slug' => $cat->slug );
And also notice your if statement. should be if( cond == cond )