开发者

Wordpress: Is there a way to only show Custom Meta Data on single posts from certain categories

开发者 https://www.devze.com 2023-03-22 19:21 出处:网络
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.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消