Wordpress: Is there a way to only show Custom Meta Data on single posts from certain categories
I am trying to use this code to display something in the 'single.php' file on the theme when the post is of a certain c开发者_JAVA百科ategory.
$cat = get_query_var('cat');
if ($cat == '4') {
echo "post";
} else {
echo "no data";
}
But it seems to ignore the $cat
var query and as such on every post display the no data message.
The cat
var is not in the URL.
I suppose wordpress don't set %cat% query variable because post may be accepted for several categories.
Try something like this:
if ( in_category(4, get_the_ID()) {
echo "post";
} else {
echo "no data";
}
Remember that you should call get_the_ID function after you call the_post
精彩评论