[英]Wordpress check if post is in category then do this… using custom categories
Ok im not sure if I have worded the title right but basically I have posts in custom categories and I want to get the current post category and do something. 好的,我不确定我的标题是否正确,但基本上我的帖子属于自定义类别,我想获取当前的帖子类别并执行某些操作。
So for example: if current post is in tag_id 15, say 'your in the category vegetarian' 例如:如果当前帖子在tag_id 15中,请说“您在素食主义者类别中”
this is what im using: 这是即时通讯使用:
http://www.example.com/wp-admin/edit-tags.php?action=edit&taxonomy=food_category&tag_ID=15&post_type=vegetarian http://www.example.com/wp-admin/edit-tags.php?action=edit&taxonomy=food_category&tag_ID=15&post_type=vegetarian
any help would be great, thank you 任何帮助都会很棒,谢谢
WordPress has a built in function for this: in_category( $category, $_post )
(used inside the loop): WordPress为此提供了一个内置函数:
in_category( $category, $_post )
(在循环内使用):
<?php if( in_category( 'vegetarian' ) ): ?>
You're in the vegetarian category
<?php endif; ?>
That's good for a specific use case. 这对于特定的用例来说是好的。 If you always want it to spit out your "You're in the [category] category" for every category, including future ones, you would use
get_the_category( $id )
like this example (inside the loop). 如果您始终希望它为每个类别(包括将来的类别)吐出“您属于[类别]类别”,则可以像下面的示例一样使用
get_the_category( $id )
(在循环内)。
<?php
$categories = get_the_category( $id );
if( $categories ){
// Assumes you just want the first category
print 'You’re in the ' . $categories[ 0 ]->name . ' category';
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.