- This topic has 7 replies, 2 voices, and was last updated 6 months, 1 week ago by
Fernando.
-
AuthorPosts
-
September 12, 2022 at 5:44 pm #2341069
Fergal
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.
September 12, 2022 at 6:02 pm #2341075Fernando Customer Support
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
September 12, 2022 at 7:47 pm #2341124Fergal
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,
FergalSeptember 12, 2022 at 7:50 pm #2341125Fernando Customer Support
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?September 12, 2022 at 8:12 pm #2341132Fergal
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?
September 12, 2022 at 8:16 pm #2341135Fernando Customer Support
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.September 12, 2022 at 8:20 pm #2341138Fergal
That worked perfectly. Thanks so much Fernando!
September 12, 2022 at 8:23 pm #2341140Fernando Customer Support
You’re welcome Fergal!
-
AuthorPosts
- You must be logged in to reply to this topic.