【问题标题】:Wordpress category/post color menuWordpress 类别/帖子颜色菜单
【发布时间】:2016-12-02 13:23:19
【问题描述】:

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

【问题讨论】:

  • 您是否尝试阅读Wordpress documentation and tutorials
  • @Dragos 看不懂,求帮助
  • 听起来你需要雇佣懂的人。
  • @vico 不明白?好吧,我在上面已经解释过,我想像上面的示例一样使用颜色菜单!我想像你在上面看到的那样做我的页面颜色作为示例。
  • 不,我的意思是你应该得到一个主题或雇用至少对 wordpress 有一定了解的人

标签: php css wordpress


【解决方案1】:

这在很大程度上取决于您的主题是如何设置的,但这里是一个总体概述:

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() 函数也可以被过滤以添加页面 slug 的类。将以下内容添加到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;
}

结论

这应该会给你大致的想法;如果不查看网站本身或不知道您使用的是哪个主题,我们无法为您提供更具体的说明。

【讨论】:

  • 这是我的页面,所以我们创建了类别而不仅仅是类别页面!网站:kosovatimes.com我们更改了颜色,但不在文章类别中:kosovatimes.com/lajme/…
  • @Vlora 我已经更新了上面的答案以反映您网站的 CSS。不幸的是,我不太明白你的最后评论。我可以看到你已经让它适用于“shendeti”类别。
  • 是的,很荣幸它在一个类别中运行,并且项目现在有机会修改上面的代码以即使在没有类别的站点上也能正常工作?示例:kosovatimes.com/category/shendetsi - 它的工作示例:kosovatimes.com/shendetsi - 这是页面不起作用!
  • 我在上面进行了更新,还为页面标题添加了类,因此您也可以定位您的“shendetsi”页面。
猜你喜欢
  • 2014-01-16
  • 1970-01-01
  • 1970-01-01
  • 2016-11-22
  • 2011-03-19
  • 2018-06-16
  • 1970-01-01
  • 1970-01-01
  • 2018-04-30
相关资源
最近更新 更多