【问题标题】:Wordpress, How to change the Post Header Title to display the Post Category?Wordpress,如何更改帖子标题以显示帖子类别?
【发布时间】:2018-06-14 16:43:08
【问题描述】:

在我们使用的主题 Mesmerize-Pro 中,对于所有帖子,它会在两个位置显示帖子标题:

  1. (英雄头衔)标题和
  2. 实际的帖子标题。

示例:(链接已删除)

我很想更改最顶部的英雄标题以显示帖子类别。

我可以通过functions.php 将帖子标题更改为类别,但这不太有意义。

感谢您的帮助。

-e

详情:

.inner-header-description h1.hero-title (Top Title, want to be the Category)

.post-item .post-content-single body #page h1 (Post Title, as is)

【问题讨论】:

  • 请在您的问题中格式化代码。此外,不要链接到外部网站,而是通过发布代码 sn-p 向我们展示您迄今为止所做的工作。

标签: php wordpress wordpress-theming


【解决方案1】:

从主题来看,它如何确定英雄头衔似乎有点过于复杂。如果主题中没有覆盖选项(我没有安装它来检查),确实会出现一个 mesmerize_header_title 过滤器,您可以使用它,这显然是他们如何在主题中本地更改 WooCommerce 页面上的标题。

需要注意的一点是,一篇文章可以有很多类别,因此通常您只想显示第一个。我们可以使用get_the_category() 返回一个WP_Term 对象的数组,并将第一个对象应用于标题标题过滤器:

add_filter('mesmerize_header_title', function($title){
    if( is_single() ){
        $categories = get_the_category();
        $title      = $categories[0]->name;
    }

    return $title;
});

您应该可以将其放入functions.php 文件中,然后一切顺利。

我基于包含以下内容的 /inc/woocommerce/woocommerce.php 文件来编写此代码:

add_filter('mesmerize_header_title', function ($title) {

    if (mesmerize_is_page_template()) {
        if (is_archive() && mesmerize_get_current_template() === "woocommerce.php") {
            $title = woocommerce_page_title(false);
        }
    }

    return $title;
});

【讨论】:

  • 做到了。太感谢了。终于解脱了。 :)
猜你喜欢
  • 2022-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多