如何将当前分类/术语层次结构的HTML类添加到我的页面中以简化样式?

时间:2016-08-08 作者:OscarGuy

我已经试着将这些问题张贴到Wordpress论坛上,但没有得到回应。希望这里有人能帮助我。

Question: Catgory/Subcategory Styling

我正在尝试根据顶级类别将所有类别/子类别标题设置为不同的样式。然而,如果不单独列出每个子类别,我就想不出一种方式来设置子类别的样式。当前看起来是这样的:

#content .category-academy-awards .entry-byline,
#content .category-academy-awards-history .entry-byline,
#content .category-oscar-in-box-office-history .entry-byline,
#content .category-oscar-profile .entry-byline,
#content .category-this-day-in-oscar-history .entry-byline,
#content .category-for-your-consideration .entry-byline,
#content .category-screener-watch .entry-byline,
#content .category-oscar-preview .entry-byline,
#content .category-precursors .entry-byline,
#content .category-predictions .entry-byline,
#content .category-presenters .entry-byline,
#content .category-shortlists .entry-byline {
background-color: #c3a310;
padding: 5px;
border-top: 2px #e3c330 solid;
border-bottom: 2px #806b09 solid;
border-right: 2px #806b09 solid;
border-left: 2px #e3c330 solid;
}

#content .category-academy-awards .entry-byline a,
#content .category-academy-awards-history .entry-byline a,
#content .category-oscar-in-box-office-history .entry-byline a,
#content .category-oscar-profile .entry-byline a,
#content .category-this-day-in-oscar-history .entry-byline a,
#content .category-for-your-consideration .entry-byline a,
#content .category-screener-watch .entry-byline a,
#content .category-oscar-preview .entry-byline a,
#content .category-precursors .entry-byline a,
#content .category-predictions .entry-byline a,
#content .category-presenters .entry-byline a,
#content .category-shortlists .entry-byline a {
color: #402b09;
}

#content .category-academy-awards .entry-categories,
#content .category-academy-awards-history .entry-categories,
#content .category-oscar-in-box-office-history .entry-categories,
#content .category-oscar-profile .entry-categories,
#content .category-this-day-in-oscar-history .entry-categories,
#content .category-for-your-consideration .entry-categories,
#content .category-screener-watch .entry-categories,
#content .category-oscar-preview .entry-categories,
#content .category-precursors .entry-categories,
#content .category-predictions .entry-categories,
#content .category-presenters .entry-categories,
#content .category-shortlists .entry-categories {
background-color: #e3c330;
padding: -1px;
border-top: 1px #f3e350 solid;
border-bottom: 1px #806b09 solid;
border-right: 1px #806b09 solid;
border-left: 1px #f3e350 solid;
}

#content .category-academy-awards .entry-categories a,
#content .category-academy-awards-history .entry-categories a,
#content .category-oscar-in-box-office-history .entry-categories a,
#content .category-oscar-profile .entry-categories a,
#content .category-this-day-in-oscar-history .entry-categories a,
#content .category-for-your-consideration .entry-categories a,
#content .category-screener-watch .entry-categories a,
#content .category-oscar-preview .entry-categories a,
#content .category-precursors .entry-categories a,
#content .category-predictions .entry-categories a,
#content .category-presenters .entry-categories a,
#content .category-shortlists .entry-categories a {
padding: 4px;
}
是否有办法将样式设置为基本“.类别奥斯卡奖”和所有子类别,或者以上是唯一可行的方法?

1 个回复
SO网友:Andy Macaulay-Brook

注意-在你给我一些反馈后,我可能需要编辑此内容。

这里的诀窍是使用body_classpost_class 过滤器。您的主题(应该)使用body_class 函数向HTML标记和互补post_class 函数可以对围绕您的主要帖子内容的任何标签执行相同的操作。它们的过滤器允许您用所需的任何内容扩展这些类列表。

因为您的CSS示例有如下选择器#content .category-academy-awards .entry-categories a 我猜我们在谈论post_class 谢谢你的提问。两者的工作方式完全相同。

在您的功能中。php文件,从以下内容开始:

add_filter(\'post_class\',\'wpse_235350_custom_body_class\');

function wpse_235350_custom_body_class( $classes ) {

    return $classes;

}
现在,当WP生成一个页面并点击post_class 函数,它将转移到此函数,传递类列表,然后该函数返回类列表,WP继续,将数组转换为一个由空格分隔的字符串,并将其推送到页面的HTML中。

还没有什么聪明的地方,但现在我们将扩展该函数,以便将其添加到类列表中。

我假设在循环中,你想找出每个帖子的类别,然后尝试找到每个帖子的顶级类别,并将该顶级类别添加到类列表中。例如,如果帖子属于类别predictions 然后我们将在树上查找顶级类别是academy-awards 因此,将其作为一个类添加到帖子中。

在我自己的编码中,我可能会比这里要浓缩更多的内容,并使用更少的临时变量,但这会导致不太清楚的答案。

首先,我们需要了解该职位的类别:

$categories = get_the_category();
这将返回类别对象的数组。我们将从此数组中提取类别ID:

if( count( $categories ) ) {
// as long as there is at least one category...

    $catlist = array(); // good to make sure it really is empty to start with

    foreach( $categories as $category ) {
        $catlist[] = (int)$category->term_id;
        // WP gets stroppy if you accidentally fail to use integers, so we co-erce the value here.
    }

}
现在我们要用真正漂亮的get_ancestors 查找每个类别的所有祖先。get_ancestors 将拉出给定对象的所有祖先的列表,无论是term 或apost 或acomment.

$fullcatlist = array();

foreach( $catlist as $category ) {

    $fullcatlist = array_merge( $fullcatlist, get_ancestors( $category, \'category\' ) );

}

// now $fullcatlist is a one dimensional array of all the ancestors\' IDs, possibly with duplicate values, so...

$fullcatlist = array_unique($fullcatlist);
现在让我们把所有的鼻涕虫都拿出来。我们获取所有的category对象,并将slug添加到一个数组中,将字符串“category-”放在每个类的前面。

$catsluglist = array();

foreach( $fullcatlist as $category ) {
    $category = get_category( $category );
    $catsluglist[] = \'category-\' . $category->slug;
}
现在我们有了它:当前帖子所有祖先类别的所有鼻涕虫的列表。

综上所述,我们有:

add_filter(\'post_class\',\'wpse_235350_custom_body_class\');

function wpse_235350_custom_body_class( $classes ) {

    $categories = get_the_category();

    if( count( $categories ) ) {
    // as long as there is at least one category...

        $catlist = array(); // good to make sure it really is empty to start with

        foreach( $categories as $category ) {
            $catlist[] = (int)$category->term_id;
            // WP gets stroppy if you accidentally fail to use integers, so we co-erce the value here.
        }

    }

    $fullcatlist = array();

    foreach( $catlist as $category ) {

        $fullcatlist = array_merge( $fullcatlist, get_ancestors( $category, \'category\' ) );

    }

    // now $fullcatlist is a one dimensional array of all the ancestors\' IDs, possibly with duplicate values, so...

    $fullcatlist = array_unique($fullcatlist);

    $catsluglist = array();

    foreach( $fullcatlist as $category ) {
        $category = get_category( $category );
        $catsluglist[] = \'category-\' . $category->slug;
    }

    return $classes;

}
它未经测试,我很乐意帮助调试。我的那杯酒呢。。。

哦,最后一点。你的所有帖子现在都应该有这个类category-academy-awards 在它们上,如果这是顶级祖先,以及任何中间祖先类别。

相关推荐

Custom Taxonomy in REST API

我有两个自定义分类法,用于我的帖子。我正试图从其中一个中检索名称。在REST API V2中,我执行以下操作:https://example.com/wp-json/wp/v2/posts/14340/?_embed=wp:term 这嵌入了所有分类法的实际名称(类别加上我的两个自定义分类法),但我只对与每个帖子相关联的名称感兴趣,只对其中一个自定义分类法感兴趣。响应示例:"_embedded": { "wp:term": [