Site logo

Archived topic

Include Page Title and Page Number on Posts Page

7 replies · Started by Philip on March 19, 2023

Viewing posts 1–8 of 8

From what I see, the posts page by default does not include an h1 title. I can add it as a hook, which I did with the following:
<h1> <?php single_post_title(); echo ' - Page '. ( get_query_var('paged') ? get_query_var('paged') : 1 );?> </h1>

But I would like this for the posts page, archives page, search results page. However, those already have h1 titles, so it won't work if I just enable it from the Display Rules > Location.

Also, is there a way to modify to just show page numbers after page 1? Basically the goal is to eliminate duplicate h1 titles on the site.

Thanks

Maybe. I might not be sure how to apply it. Basically, I want to show the page title as an H1 before the content.

For example on the Blog page, before it lists all the posts, I would want it to say "Blog". So I created a hook element to generate_before_main_content. That works, but ideally I would have liked it to say page 1 of 100, etc.

Also, was wondering how I can do the same for the archive pages as well.

Hi Philip,

To clarify, on Page 1, do you want to show the actual title? And, on page 2 onwards, show the Page number as the page title?

If you're referring to something else, let us know.

That's exactly it. I wanted to do it for the Blog, Category Archives, Author Archives, etc.

What you're doing should be fine.

Create a Block Element for the Title on the first page. Then, add something like this:

add_filter( 'generate_element_display', function( $display, $element_id ) {
    if ( 100 === $element_id && is_paged() ) {
        $display = false;
    }

    return $display;
}, 10, 2 );

Replace 100 with your Element's ID. Getting the Element ID: https://share.getcloudapp.com/YEuDdrnQ

Adding PHP: https://docs.generatepress.com/article/adding-php/#code-snippets

Then, for your Hook Element where you're adding your code, add this snippet as well:

add_filter( 'generate_element_display', function( $display, $element_id ) {
    if ( 100 === $element_id && !is_paged() ) {
        $display = false;
    }

    return $display;
}, 10, 2 );

Again, replace 100 with your Hook Element's ID.

Got you. Would I be able to do that without the code snippets plugin? For example, just by enabling the "Execute PHP"? Because I tried that, and it didn't work.

Hi there,

that code cannot be used in a GP Hook element.
It has to be added either using that plugin, or in a child theme.

This archived topic is closed to new replies.