Site logo

Archived topic

Display Archive Title & Description when Using Query Loop

13 replies · Started by Ian on November 2, 2022

Viewing posts 1–14 of 14

Using the new Loop Content and Query Loop to style archives, search and blog. How do we display the regular Archive Title + Description when using the Query Loop to create archive templates?

I think I see these 2 WordPress blocks that will work for Archives.

Archive Title
Term Description

How do we automate titles for Search results? Something similar to the default search results title?

Hi Ian,

To clarify first, are these inquiries in reference to using the Block Element - Loop Template?

If so,

1. To get the Title, create a GB Headline Block, and enable the dynamic data in the tool bar settings of the Block. Then, set the dynamic text type to title. Example: https://share.getcloudapp.com/P8u742ep

2. To get the Description, you can use a Dynamic Content Block. Set the type to term description. Reference: https://docs.generatepress.com/article/dynamic-blocks/#dynamic-content

3. Create a Headline Block. Enable dynamic data as done in step one. Add my-search-title to the class list of the Headline Block. Lastly, add this PHP snippet:

add_filter( 'generate_dynamic_element_text', function( $text, $block ) {
    if ( !is_admin() && is_search() && ! empty( $block['attrs']['className'] ) && 'my-search-title' === $block['attrs']['className'] ) {
        $text = 'Search Results: ' . get_search_query();
    }

    return $text;
}, 10, 2 );

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

Hi Fernando,

I think we may discussing 2 separate things. #1 and #2 are the archive titles and archive descriptions, not within the Loop Content. I was not able to use any GenerateBlocks to display these. But I was able to use the standard WordPress Archive Title, Term Description to them for archive templates. And since Blog and Search Results not have have these, there is no H1 Title section.

And thanks for the snippet. I now see a title at the top of Search Results. Now is is possible to show the default search title like:

Search Results for: search-term

Hi Fernando, I should clarify.

#1. I noticed there is a prefix that we can add so I can achieve "Search Results:" but the search term following it is not displaying the proper search term. Looks to be a title from one of the results. Having said that, usually when I build archives, I also target blog and search results. Using the GB blocks, I see Search Results in archive results. So will stick to using the default WP blocks for title and description.

#2. I can now get the dynamic content block to display term description. Thank you for this.

I updated the code above. Can you test that for the Search title?

Thanks for updating the snipper. I see the default Search Results: search term now. Thank you!

Is it possible to only display this headline when on the search results page and not the archive or blog page? I normally run this same template for:

Archives
Search Results
Blog

It would be handy to run one instead of having to create a second Loop Element for Search Results.

TIA Fernando! You are awesome!

Hi Fernando. I did on my last reply. Let me try again in this reply. I did upload a different logo to the merged header inside Elements. Pretty sure I have done this in previous sites but never ran into this. Thx!

Why not just keep the GB Headline Block and remove the Archive Title Block? That should keep the Title to just one for these pages.

Thanks. Let me keep at it. I was testing it on another site to make sure I got everything to work. I will port your setup over to this staging site tomorrow. Awesome sauce and thank you!

You're welcome Ian!

Hi Fernando,

Your instructions and snippet worked beautifully. It allowed me to keep targeting archives, search results and blog using the same template and still allow archive title and description to display properly as well as search results to display when needed. Excellent. Thank you!

You're welcome Ian! Glad to be of assistance!

This archived topic is closed to new replies.