• Resolved ulrichnielsen

    (@ulrichnielsen)


    Hi,

    the title is not showing on archive pages, which I think is a WP thing and not theme related. I googled this and only found solutions that suggest using a block element. I’m not using gutenberg at all.

    I already got the tag and category titles to show with this:

    add_filter( 'get_the_archive_title', function( $title ) {
        if ( is_category() ) {
            $title = single_cat_title();
        }
        elseif ( is_tag() ) {
    	$title = single_tag_title();
        }
        return $title;
    }, 50 );

    The only thing I still need is the title on the blog page itself (not the single post page, it’s fine). Can you pls provide a simple snippet to achieve this?

    Thanks!

Viewing 5 replies - 1 through 5 (of 5 total)
  • ying

    (@yingscarlett)

    Hi there,

    Try this snippet, replace Blog with the actual title you want.

    add_action('generate_before_main_content', function() {
    	if (is_home()) {
    	echo '<header class="page-header" aria-label="Page">
              <h1 class="page-title">Blog</h1>
    		  </header>';
    	}
    });
    Thread Starter ulrichnielsen

    (@ulrichnielsen)

    Thanks much @yingscarlett, it works, although I was hoping to find a solution similar to the one I posted above. Half of my problem was that I didn’t know is_home returns true even if the blog page is not the home page. I checked the WP reference and it was indeed trivial.

    But I don’t get it how the blog page is different from archive pages like tags, categories, search. They all can display the title without injecting extra markup. If this went into the header template it’d be fine but in functions.php it’s sort of a hack.

    This is what I finally added:

     echo '<h1 class="entry-title">' . get_the_title(19) . '</h1>';

    I know it’s an extra db query but a hard coded title just didn’t feel elegant.

    ying

    (@yingscarlett)

    You are using the page id to pull the title, it doesn’t seem dynamic either.

    Didn’t know the front page is not the latest posts, in this case, here’s the updated code:

    add_action('generate_before_main_content', function() {
        if (is_home() && get_option('page_for_posts')) { 
            $blog_page_id = get_option('page_for_posts');
            $blog_page_title = get_the_title($blog_page_id);
            
            echo '<header class="page-header" aria-label="Page">
                <h1 class="page-title">'. $blog_page_title.'</h1>
            </header>';
        }
    });
    Thread Starter ulrichnielsen

    (@ulrichnielsen)

    Wow this is perfect, thank you so much!

    ying

    (@yingscarlett)

    You are welcome ? ??

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Show title on blog page’ is closed to new replies.