WordPress类别/发布颜色菜单



您能告诉我如何制作帖子WordPress类别和不同的颜色吗?我想成为不同颜色的唯一菜单以及项目的类别!一个例子:在此处查看此网站,您喜欢在我们发布类别时更改颜色设置。

其中很多取决于您的主题的设置方式,但这是一般概述:

1。确保您正在使用Body_Class()函数

检查您的主题header.php并确保body标签看起来像:

<body <?php body_class(); ?>>

这将自动在您的身体标签中添加一系列类,包括根据您要查看的类别存档页面的类。

2。使用过滤器将类别类添加到单个帖子

将以下功能插入主题的functions.php文件:

function my_body_class_add_categories( $classes ) {
    // Only proceed if we're on a single post page
    if ( !is_single() )
    return $classes;
    // Get the categories that are assigned to this post
    $post_categories = get_the_category();
    // Loop over each category in the $categories array
    foreach( $post_categories as $current_category ) {
        // Add the current category's slug to the $body_classes array
        $classes[] = 'category-' . $current_category->slug;
    }
    // Finally, return the $body_classes array
    return $classes;
}
add_filter( 'body_class', 'my_body_class_add_categories' );

这还将将类别类添加到单个帖子中。

3。添加页面

也可以过滤body_class()功能以添加页面slugs的类。将以下内容添加到functions.php

function my_body_class_add_page_slug( $classes ) {
    global $post;
    if ( isset( $post ) ) {
        $classes[] = $post->post_type . '-' . $post->post_name;
    }
    return $classes;
   }
   add_filter( 'body_class', 'my_body_class_add_page_slug' );

这将将page-title类添加到身体中。

4。随便你

这将根据您的主题标记而有所不同,但它将沿线

.td-header-main-menu {
    background: blue; // The fallback colour for all pages
}
.category-showbiz .td-header-main-menu {
    background: red;
}
.category-sport .td-header-main-menu {
    background: yellow;
}
.category-shendetsi .td-header-main-menu,
.page-shendetsi .td-header-main-menu {
    background: green;
}

结论

应该给您一般的想法;我们不能在不看到网站本身或知道您正在使用的主题的情况下给您更具体的说明。

最新更新