Site logo

Archived topic

Search Results Page Title Is Random

9 replies · Started by Harvey on May 19, 2022

Viewing posts 1–10 of 10

G'day guys,

I'm using the Search theme and having a small issue with my search results page.

If you head to http://volleyballvault.com/s=anything , you'll see that the header/h1 of the page just pulls the title of the first article in the list and puts that as the header.

I'd prefer if this header showed the 'Search results for: x' up there or was blank or fixed. It doesn't make much sense to have a random search result title as the page title.

If it's relevant, I'm disabling page titles for each of the posts on the page so that they don't display twice - not sure if this is relevant.

Thanks so much for any help!
Harvey

Hey Leo,

I'm afk right now so can't put search in the nav, but you can access the search bar easily by typing in any messed up URL. Like VolleyballVault.com/whatever ,

Then simply type whatever you like into the search bar and it'll take you to the SRP.

From there you'll see how the top header on the page isn't functioning as you mention.

Cheers!

Hi there,

try this:

1. In Appearance > Elements edit the element titled: Global
1.1 Select the Headline block that is displaying the title, and in Advanced > Additional CSS Class(es) add: hero-title
1.2 Save the changes.

2. Add this PHP Snippet to your site:

add_filter( 'generate_dynamic_element_text', function( $text, $block ) {
    if ( is_search() && ! empty( $block['attrs']['className'] ) && 'hero-title' === $block['attrs']['className'] ) {
        $text = get_search_query();
    }

    return $text;
}, 10, 2 );

This doc explains how to add PHP: https://docs.generatepress.com/article/adding-php/

Fantastic stuff, David!

That did almost exactly what I wanted.

I'm wondering if it's not going to be too difficult for you to put together, how difficult it would be to remove the second 'search results for:' part on the page, as that info is now in the first header.

https://i.imgur.com/y8xNbDx.jpg

I modified the code slightly to add some text before the output - but now it seems kind of redundant to have it there twice.

If it'll be too much of a pain in the ass, don't sweat it. You've been really helpful already!

Cheers,
Harvey

Ah ok - add this snippet to remove that:

remove_action( 'generate_before_loop', 'generate_do_search_results_title', 25 );

That didn't seem to make any difference unfortunately.

It's all good though, I don't want to waste your time on something like this. I'm happy enough with how it's looking. :)

Thanks to the both of you for such awesome support!

Try changing it to:

add_action('wp', function(){
    remove_action( 'generate_before_loop', 'generate_do_search_results_title');
});

Absolutely nailed it!

Thanks so much David!!!

Glad to be of help!

This archived topic is closed to new replies.