-
Paully
I have created an all archives page hero element. I have added a headline and clicked on the dynamic options and selected title to pull in the title of the category. This works fine when there is a post assigned to that category but when there are zero posts assigned that category, the category title doesn’t show at all.
Is there a way for the category title to show dynamically on archive pages even if there aren’t posts assigned to it yet?
-
Fernando
Hi Paully,
That’s the default behavior. Here’s something you can try: https://generatepress.com/forums/topic/empty-category-archive-page/#:~:text=I%20just%20realized%20you%E2%80%99re
-
Paully
Thanks for your fast reply. I did try that but it seems I am then unable to style the shortcode. Is there another solution please?
-
Fernando
Try adding a GB Headline Block with class
get-archive-title
. Adding Custom Classes: https://wordpress.com/support/wordpress-editor/adding-additional-css-classes-to-blocks/Then, add this code:
add_filter('generateblocks_dynamic_content_output', function($content, $attributes, $block){ if ( ! is_admin() && ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'get-archive-title' ) !== false ) { if(! $content){ return get_the_archive_title(); } } return $content; }, 10, 3);
Adding PHP: https://docs.generatepress.com/article/adding-php/#code-snippets
-
Paully
Thanks again. I followed your instructions with both, cleared the cache but still not title. Have I missed something?
-
Fernando
Could you share admin login credentials? I’ll check why it isn’t working.
-
Paully
Thanks. As requested in the private area.
-
Fernando
Disable the dynamic option feature. Just any text.
Then, replace the PHP snippet with this:
add_filter( 'render_block', function( $block_content, $block ) { if ( !is_admin() && ! empty( $block['attrs']['className'] ) && strpos( $block['attrs']['className'], 'get-archive-title' ) !== false ) { $myreplace = '/>(.*?)<\/h1>/s'; $myinsert = get_the_archive_title(); $block_content = str_replace( $myreplace, $myinsert , $block_content ); } return $block_content; }, 10, 2 );
-
Paully
Thanks, is this snippet instead of the previous one, or in addition?
-
Fernando
Replace the previous one with that.
-
Paully
After following those instructions, all I see now is the text that I have replaced it with instead of the dynamic title.
-
Fernando
I replaced it with this:
add_filter( 'render_block', function( $block_content, $block ) { if ( !is_admin() && ! empty( $block['attrs']['className'] ) && strpos( $block['attrs']['className'], 'get-archive-title' ) !== false ) { $myreplace = 'TEST'; $myinsert = get_the_archive_title(); $block_content = str_replace( $myreplace, $myinsert , $block_content ); } return $block_content; }, 10, 2 );
I just used the static text you added – TEST – as the identifier.
-
Paully
I understand now thank you. It does however pose another challenge in that I am now not able to use the before text feature as I was doing in the dynamic option.
-
Fernando
You can modify the code to this:
add_filter( 'render_block', function( $block_content, $block ) { if ( !is_admin() && ! empty( $block['attrs']['className'] ) && strpos( $block['attrs']['className'], 'get-archive-title' ) !== false ) { $myreplace = 'TEST'; $myinsert = 'YOUR BEFORE TEXT HERE ' . get_the_archive_title(); $block_content = str_replace( $myreplace, $myinsert , $block_content ); } return $block_content; }, 10, 2 );
-
Paully
Thank you, that worked great.
-
Fernando
You’re welcome!
- You must be logged in to reply to this topic.