wordpress判断当前文章的分类i_category

小初seo 网站建设评论827字数 2623阅读8分44秒阅读模式

最近自己在修改一个采用Wordpress程序的博客的时候需要用到一个特殊的功能:我需要判断这篇文章是属于哪些分类,如果属于我设定的分类下的文章,则输出一个DIV内容。按道理说实现这个功能应该不算太难,因为印象中wordpress有相关的函数。简单查阅了一些资料后发现is_category和in_category这两个函数,最后是靠in_category函数实现的。具体方法也很简单:

in_category(array( ’1′, ’2′, ’3′)) ) 这段函数的意思是识别分类目录ID为1、2、3这三个分类,可以利用这个功能实现特定分类使用自定义模板或内容等。

这其中还有个小插曲让我纠结了很久,那就是in_category和is_category的差别,它们的差别让我想实现的功能差点就黄掉了。这里贴出来做个记录:

in_category:判断当前文章或指定文章是否属于某个指定类别,只有直属的类别,不包括直属类别的父辈类别;可以在循环内使用,也可以独立使用。

is_category:判断是否正在显示一个类别归档页面。

也就是说,如果你要在wordpress里面判断某个东西是否属于某分类,则用in_category,而如果是想判断某个分类的表现,那就要用is_category函数。

例子

在循环中测试当前帖子

in_category() 通常用于根据当前帖子的类别在循环中采取不同的操作,例如

<? php 
 if ( in_category( 'pachyderms' )) {
     //它们的树干很长... 
} elseif ( in_category( array ( 'Tropical Birds', 'small-mammals' ) )) {
     //它们是温血动物.. . 
} else {
     // & c. 
}
 ?>

在循环外测试当前帖子

在单个帖子的请求期间(通常由 single.php 模板处理),您甚至可以在循环开始之前测试该帖子的类别。

您可以使用它来切换模板,如下所示:

<? php
 if ( in_category('fruit' ) ) {
     include 'single-fruit.php' ;
} elseif ( in_category('vegetables' ) ) {
    包括'single-vegetables.php' ;
} else {
     //继续正常循环
    if ( have_posts() ) : while ( have_posts() ) : the_post();
    // ... 
}
 ?>

( 自定义帖子模板插件 允许为单个帖子创建模板。它还显示了如何添加模板的示例,该模板用于 给定类别中的所有 帖子,而不仅仅是单个帖子。该示例在默认情况下是插件,但可以通过取消注释相应的行来轻松实现。)

测试帖子是否属于后代类别

当显示类别存档,或通过 WP_Query() 或 get_posts()显示类别的帖子时,或使用 is_main_query()连接到主查询时,WordPress 从指定类别  任何后代(子)类别中检索帖子,但 in_category( ) 仅针对帖子的指定类别进行测试,而不针对这些类别的祖先(父母)进行测试。

例如,如果您有一个帖子分配给子类别 Fruit → Bananas 而不是类别 Fruit,那么 Fruit 类别存档将显示“香蕉”帖子,但调用in_category('fruit') 该帖子总是返回 false

您可以列出祖先类别和每个可能的后代类别,例如,

<?php if ( in_category( array ( 'fruits', 'apples', 'bananas', 'cantaloupes', 'guava', /* etc */ ) )) {
     //这些都是水果
}
 ?>

但是每次移动或添加任何“水果”类别时都必须编辑代码。

更灵活的方法是使用或改编下面定义的 post_is_in_descendant_category 函数(调用前需要将下面的函数定义复制到模板、插件或主题函数文件中)。您可以像这样将它与 in_category()一起使用 (在此示例中,11 是“水果”类别的 ID 号):

// Post 是分配给“fruit”类别还是“fruit”类别的任何后代?
<?php if ( in_category( 'fruit' ) || post_is_in_descendant_category( 11 ) ) {
     //这些都是水果... 
}
 ?>

如果您更愿意按名称引用类别,则可以使用,例如,

if ( $category_to_check = get_term_by( 'name', 'fruit', 'category' ))
   post_is_in_descendant_category( $category_to_check ->term_id);

post_is_in_descendant_category 函数

<? php
 /* *
 * 测试任何帖子的分配类别是否是目标类别的后代
 *
 * @param int|array $cats 目标类别。整数 ID 或整数 ID 数组
 * @param int|object $_post 帖子。省略在循环或主查询中测试当前帖子
 * @return bool 如果帖子的至少 1 个类别是任何目标类别的后代,则为真
 * @see get_term_by() 可以通过名称或slug获取类别,然后将ID传递给该函数
 * @uses get_term_children() 传递 $cats
 * @uses in_category() 传递 $_post(可以为空)
 * @2.7 版
 * @link http://codex.wordpress.org/Function_Reference/in_category#Testing_if_a_post_is_in_a_descendant_category
 */
if ( ! function_exists( 'post_is_in_descendant_category' ) ) {
    function post_is_in_descendant_category( $cats, $_post = null ) {
        foreach ( (array) $cats as $cat ) {
            // get_term_children() accepts integer ID only
            $descendants = get_term_children( (int) $cat, 'category' );
            if ( $descendants && in_category( $descendants, $_post ) )
                return true;
        }
        return false;
    }
}
?>

 
  • 本文由 小初seo 发表于 2022年7月20日12:24:14
  • 转载请务必保留本文链接:https://www.pkak.cn/web/8654.html
匿名

发表评论

匿名网友
:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

拖动滑块以完成验证