doutou7286 2018-06-14 16:43
浏览 45
已采纳

Wordpress,如何更改帖子标题以显示帖子类别?

In the theme we're using, Mesmerize-Pro, for all posts, it's displaying the post title in two places:

  1. The (hero title) header, and
  2. The actual post title.

Example: (Link Removed)

I'd love to change the top most hero title to instead display the post category.

I'm able to change the post title to the category, via functions.php, but that makes less sense.

Thank you for any help.

-e

Details:

.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)
  • 写回答

1条回答 默认 最新

  • donglongqiao9595 2018-06-14 16:56
    关注

    Looking at the theme, it seems a bit overly complex in how it determines the hero title. If there's no override options in the theme (I didn't install it to check), there does appear to a mesmerize_header_title filter you can hook into, it's apparently how they change the title on WooCommerce pages natively in the theme.

    One thing to note is that there can be many categories on a post, so typically you'll only want to show the first one. We can use get_the_category() to return an array of WP_Term objects, and apply the first one to the header title filter:

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

    You should be able to put this in the functions.php file and be good to go.

    I based this code off the /inc/woocommerce/woocommerce.php file that contains the following:

    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;
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?