Site logo

[Support request] Dynamic Post Category Term in Page Hero Element

Home Forums Support [Support request] Dynamic Post Category Term in Page Hero Element

Home Forums Support Dynamic Post Category Term in Page Hero Element

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #2457276
    Tim Taricco

    I recently received support help in regards to a default “no posts found” message when no posts match the query when using the GenerateBlocks Query Loops block: https://wordpress.org/support/topic/query-loop-block-default-output-when-no-posts/#post-16273656. This solution feels like a bandaid, but it works. The GB Query Loop block should have a field for a default “no posts found” message.

    In a related issue, when using the Page Hero element type in an archive template and the GB Headline block with dynamic content (Title), if there are no posts returned in the query, the dynamic content in the GB Headline block doesn’t render = the Title is blank.

    In an ideal world, the dynamic text (Title) should render in the GB Headline block, and the “no posts found” message would show.

    #2457976
    David
    Staff
    Customer Support

    Hi there,

    we are looking at ways to handle this without custom code or adding proprietary fields and it is high on our list of to dos.

    Do you need assistance today for making changes to the no results archive ?

    #2459242
    Tim Taricco

    If you have a solution to the Archive template dynamic text Title being empty when the post query has no results, that would be great.

    #2459349
    David
    Staff
    Customer Support

    Can you confirm are you using the GP Dynamic Data ( set in the Block Toobar ) ?

    #2459431
    Tim Taricco

    Yes, “Title” is selected in the GP Dynamic Data section of the toolbar.

    #2459757
    Fernando
    Customer Support

    Hi Tim,

    When there are no posts in an archive page, it uses the no-results.php template by default.

    To add a “No Posts Found” title, you can add another Headline Block for the “No Posts Found” Text. Then, give it a class of cu-no-posts-headline through the Advanced section of the Block Settings. Adding Custom Classes: https://wordpress.com/support/wordpress-editor/adding-additional-css-classes-to-blocks/

    After doing so, add this PHP:

    add_filter( 'render_block', function( $block_content, $block ) {
        if (!is_admin() && ! empty( $block['attrs']['className'] ) && strpos( $block['attrs']['className'], 'cu-no-posts-headline' ) !== false && have_posts() ) {	
    		return '';	
        }
    
        return $block_content;
    }, 10, 2 );

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

    This Snippet removes the “No Posts Found” title on Archive pages with Posts and shows it if there aren’t any posts.

    #2460694
    Tim Taricco

    Hi Fernando, unfortunately, your solution doesn’t address dynamic content because the Headline block should display the category name for the specific Archive. What I have done is created a Page Hero Element and would like the Category name to show in the Headline block even when there are no query results for that category. The manual solution would be to create a separate Page Hero Element for each category, but that’s not a good solution. Anyway, that’s the reason for my support request.

    #2461791
    David
    Staff
    Customer Support

    Ok, so here is a work around.

    1. Add this PHP Snippet to your site:

    
    add_filter( 'render_block', function( $block_content, $block ) {
    
        $class = 'no-results-title'; // Additional CSS Class on block to find
        $string = 'no results'; // string of text to replace
    
        if ( 
            !is_admin()
            && ! empty( $block['attrs']['className'] )
            && $class === $block['attrs']['className']
        ) {
            if ( !have_posts() && is_archive() ){
                $replace = get_the_archive_title();
                $block_content = str_replace( $string , $replace , $block_content );
            } else {
                $block_content = null; // if there are posts remove it title
            }
        }
    
        return $block_content;
    }, 10, 2 );

    2. In your block element add a second headline block.
    Don’t set it to dynamic.
    Just add some static text that includes the: no results
    Give it an Advanced > Additional CSS class of: no-results-title

    This title will only be displayed when viewing an empty archive, and will show the Term name in its place.

    #2461908
    Tim Taricco

    Thank you. This worked perfectly.

    #2462626
    David
    Staff
    Customer Support

    Awesome – glad to be of help

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