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)

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;
});