Site logo

Archived topic

Change archive title that displays on homepage and paginated pages.

7 replies · Started by Fergal on September 12, 2022

Viewing posts 1–8 of 8

Hey there,

I'd like to replicate the archive title for pagination that I have on my .com/job-openings/ archive pages, but for my home page where latest posts are being displayed.

I'd like to display it like:

  • "All Jobs" on the home page
  • "All Jobs - Page 2" on page 2 (.com/page/2)
  • "All Jobs - Page 3" on page 3 (.com/page/3)

I was able to achieve this on the .com/job-openings/ archive pages using the following code. I imagine this can be repurposed to achieve the above.

add_filter( 'get_the_archive_title', function( $title ) {
    if(is_post_type_archive('job-opening')) {
        the_post();
        //global $page;
        $paged = get_query_var('paged') ? get_query_var('paged') : 1;
        $title = sprintf('All Jobs - Page %1$s', $paged);

        rewind_posts();
    }

    return $title;
} );

Thanks for your help.

Hi Fergal,

Create a new Block Element. Add a GB Headline Block with class my-blog-title- You can add this through the Advanced section of the Blog settings.

Set the text to "All Jobs". Display it on your Front page. Choose a hook you prefer as well.

Then, add this Snippet:

add_filter( 'render_block', function( $block_content, $block ) {
    if ( !is_admin() && ! empty( $block['attrs']['className'] ) && strpos( $block['attrs']['className'], 'my-blog-title' ) !== false && is_paged() ) {
		$myreplace1 = 'All Jobs';
		$paged = get_query_var('paged') ? get_query_var('paged') : 1;
        $myinsert1 = sprintf('All Jobs - Page %1$s', $paged);
        $block_content = str_replace( $myreplace1, $myinsert1 , $block_content );
		
		
    }

    return $block_content;
}, 10, 2 );

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

Hey Fernando,

I followed those steps, but it doesn't seem to display anything. I used the "before_main_content" hook.

How can we debug this?

Thanks,
Fergal

If you change the hook to after_header, and add a static Headline text, does it work? Can you try switching the Display Rule to Blog if not Front Page?

Awesome it is working thank you! generate_before_main_content has placed the text where I want it to go.

A quick followup hopefully. I have a couple elements that I want to just show up on the front page, but they are also showing up on .com/page/2. Is it possible to just display elements on the front page and not the blog page (.com/page/2, etc.) when my homepage is displaying latest posts?

Great!

As for the other inquiry, you'll need PHP for that. Specifically, the generate_element_display filter is what you'll need to use: https://docs.generatepress.com/article/generate_element_display/

So, basically, you'll need something like this for instance:

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

    return $display;
}, 10, 2 );

Replace 100 with your Block Element post id.

That worked perfectly. Thanks so much Fernando!

You're welcome Fergal!

This archived topic is closed to new replies.