admin管理员组

文章数量:1313822

I'm building a WordPress website using the Patch theme. On my category pages, I have:

Category: CATEGORY_NAME

text as the archive page title and I would like to change it to a custom text for each category. I have searched in Stackexchange but there doesn't seem to be a solution to this. People mostly ask for the category page title in the head section of the HTML.

One of the solutions I found was this:

function my_change_category_title( $title, $sep ) {
    if ( is_category('life') ) {
        return 'Life is life';
    }

    return $title;
}

add_filter( 'wp_title', 'my_change_category_title', 10, 2 );

Where "Life is life" is supposed to replace the current "Category: Life" title that I have. But this doesn't work.

Here is my archive.php file, which I plan to use for my category-slug.php template files:

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

get_header(); ?>

<?php if ( have_posts() ) : ?>

    <div id="primary" class="content-area">
        <main id="main" class="site-main" role="main">

            <?php get_template_part( 'loop' ); ?>

            <?php patch_paging_nav(); ?>

        </main><!-- #main -->
    </div><!-- #primary -->

<?php else : ?>

    <?php get_template_part( 'content', 'none' ); ?>

<?php endif; ?>

<?php get_footer();

That is the only change I would have to make to the code of the theme, so I would be very grateful if someone can assist me with this.

I'm building a WordPress website using the Patch theme. On my category pages, I have:

Category: CATEGORY_NAME

text as the archive page title and I would like to change it to a custom text for each category. I have searched in Stackexchange but there doesn't seem to be a solution to this. People mostly ask for the category page title in the head section of the HTML.

One of the solutions I found was this:

function my_change_category_title( $title, $sep ) {
    if ( is_category('life') ) {
        return 'Life is life';
    }

    return $title;
}

add_filter( 'wp_title', 'my_change_category_title', 10, 2 );

Where "Life is life" is supposed to replace the current "Category: Life" title that I have. But this doesn't work.

Here is my archive.php file, which I plan to use for my category-slug.php template files:

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

get_header(); ?>

<?php if ( have_posts() ) : ?>

    <div id="primary" class="content-area">
        <main id="main" class="site-main" role="main">

            <?php get_template_part( 'loop' ); ?>

            <?php patch_paging_nav(); ?>

        </main><!-- #main -->
    </div><!-- #primary -->

<?php else : ?>

    <?php get_template_part( 'content', 'none' ); ?>

<?php endif; ?>

<?php get_footer();

That is the only change I would have to make to the code of the theme, so I would be very grateful if someone can assist me with this.

Share Improve this question edited Nov 29, 2020 at 10:26 Todor Atanasov asked Nov 24, 2020 at 18:29 Todor AtanasovTodor Atanasov 11 silver badge2 bronze badges 2
  • Wouldn't it be easier to just change the category name to what you're looking for? You could keep the slug the same and overwrite it whenever adding it to menus. – Howdy_McGee Commented Nov 25, 2020 at 23:13
  • Well, I guess I could do that. However, I would still have to get rid of the "Category:" part in the page title. Also, the Category is not printed in the archive.php file, so I cannot override it like this in my category-slug.php template file. I will edit the post to show the archive.php file of the theme. – Todor Atanasov Commented Nov 29, 2020 at 10:24
Add a comment  | 

1 Answer 1

Reset to default 0

Not the best solution, but still works for my needs.

I used the category description instead, with h1 tags so that it looks like a heading. Then removed the taxonomy page title with:

function my_theme_archive_title( $title ) {

    return "";
}

add_filter( 'get_the_archive_title', 'my_theme_archive_title' );

本文标签: plugin developmentOverride category archive page title (not the head title)