2

How to add Category name to post title (H1)? “PostTitle + CategoryName”?

My h1 post title code:

<?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
  • 1
    Remember a post can be in multiple categories. – bravokeyl Jun 8 '18 at 3:21
0

I would work the code arround and do something like this.

<h1><?php echo get_the_title(); ?> - <?php the_category(', '); ?></h1>

get_the_title(); Retrieve post title.

the_category(', '); Displays links to categories, each category separated by a comma (if more than one).

Documentation can be founded here : https://codex.wordpress.org/Function_Reference/the_category

Hope it helps you.

| improve this answer | |
  • Why echo get_the_title(); and not just the_title()? – Krzysiek Dróżdż Jun 8 '18 at 8:32
  • I guess it's a developper preference right here when you do not need to work with the before and after filter for the title. At this point of just rendering a string, it would be how the person like to code his or her theme based on his or her way of learning. – Patrice Poliquin Jun 8 '18 at 11:45
0

There are two solutions you can use.

Change it in the theme

If you can just modify the line that is printing the title, then do it. You can replace it with:

<h1><?php the_title(); ?> - <?php the_category(); ?></h1>

And if the categories shouldn't be linked, then you can use this:

<h1><?php the_title(); ?> - <?php
    $categories = get_the_category();
    if ( ! empty($categories) ) {
        foreach ( $categories as $term ) {
            echo esc_html( $term->name ) . ' ';
        }
    }
?></h1>

If you can't change the theme

Or you don't want to, because there are many, many places you'd have to change, then you can use the_title filter:

function add_categories_to_the_title( $title, $id ) {
    if ( is_singular( 'post' ) && 'post' == get_post_type( $id ) ) {  // it will work only for posts on single page, but you can modify this condition
        $categories = get_the_category();
        $cats_string = '';
        if ( ! empty($categories) ) {
            foreach ( $categories as $term ) {
                $cats_string .= $term->name . ' ';
            }
        }
        if ( $cats_string ) {
            $title .= ' - ' . $cats_string;
        }
    }

    return $title;
}
add_filter( 'the_title', 'add_categories_to_the_title', 10, 2 );
| improve this answer | |
  • I like the_title filter, but here is a bug. Why it adds category names not only to post titles but also to category bar in the menu :( – Seryozha Jun 8 '18 at 11:16
  • @Seryozha because that’s the condition. You didn’t mention what titles and where exactly you want to change, so... – Krzysiek Dróżdż Jun 8 '18 at 11:17
  • Thanks for the reply. Can you please tell me, how to add category names only in posts, but not in the navigation menu with "the_title filter"? – Seryozha Jun 8 '18 at 11:22
  • Should they be modified on archives also? – Krzysiek Dróżdż Jun 8 '18 at 11:23
  • No, only in single posts and only in h1 – Seryozha Jun 8 '18 at 11:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.