Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Dynamic Archive Title Disappears Without Post Assigned To That Category

  • 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?

  • 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?

  • 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

  • Thanks again. I followed your instructions with both, cleared the cache but still not title. Have I missed something?

  • Could you share admin login credentials? I’ll check why it isn’t working.

  • Thanks. As requested in the private area.

  • 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 );
  • Thanks, is this snippet instead of the previous one, or in addition?

  • Replace the previous one with that.

  • After following those instructions, all I see now is the text that I have replaced it with instead of the dynamic title.

  • 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.

  • 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.

  • 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 );
  • Thank you, that worked great.

  • You’re welcome!

Viewing 16 posts - 1 through 16 (of 16 total)
  • You must be logged in to reply to this topic.