Site logo

Archived topic

Need to style only native search results

5 replies · Started by Tobias on August 31, 2022

Viewing posts 1–6 of 6

Hi,

I've successfully styled the native WP search results using a Block Element of type Content Template.

A plugin (GeoDirectory) has set up a custom search, that is based on a usual page with a shortcode in it.

Here comes the problem as I understand it:

  • The custom search results are included in a normal page that hold a Shortcode. That page begins with an <article> tag.
  • The block element styles all <article> elements in search result pages, even in custom searches.
  • The result is, the whole custom search page gets the styling (background, colors) of a single search result.

I hope my analysis makes sense. Feel free to ask.

What I need are ideas about how to exclude the content template from custom search pages.

Thanks!

Hi Tobias,

Can you show us a custom search page and the default search result page?

Let me know!

Hi Ying,

Please find both links in the private area of this post.

Look at the custom search source code: All content is covered by an article.dynamic-content-template (the template element). It surrounds the search results altogether! The template element applies the content of the last search result to the whole page. There is a hidden image (display:none) above the H2 title of the page, that corresponds to the last search result.

I want to exclude the custom search page completely, because its content is managed by the 3rd party plugin. But excluding "all singular" or "all pages" didn't do the job.

If there's a chance to add a feature to elements like "exclude by body-class", that would be a very versatile solution!

Thanks in advance for taking the time.

Hi there,

you can use the generate_element_display filter hook to change the display of an element:

https://docs.generatepress.com/article/generate_element_display/

And according to this reply it looks like we can use:

geodir_is_geodir_page() to check if we're on a geodir page.

So putting it altogether you can use this snippet:


add_filter( 'generate_element_display', function( $display, $element_id ) {
    if ( 100 === $element_id && geodir_is_geodir_page() ) {
        $display = false;
    }

    return $display;
}, 10, 2 );

You need to change the 100 to match the ID of the Block Element you want to disable.

Hi David,

That code works like a charm!

Thanks :)

Glad to hear that

This archived topic is closed to new replies.