[Resolved] Customize the search result and its title.

Home Forums Support [Resolved] Customize the search result and its title.

Home Forums Support Customize the search result and its title.

Viewing 14 posts - 1 through 14 (of 14 total)
  • Author
    Posts
  • #2024440
    Beatriz

    Hi, I have three questions about how to display the search result:

    1. In the presentation of my project category I have used one of the Content Templates and I like it very much (link 1), however in my search result the same template does not apply. I would like to create a new template for that result, so that I get a simple listing in one column and not in three, as in Projects. How can I present my search results in a single column?

    2. In the title of the search result, I wanted to change it with template block, however I get my first element, and what I really want to change is the title, i.e.: “Search result for : ” see my link 2

    3. In the search result I would like to customize it so that it only shows me the pages, and articles and not the directories, or categories that I would like to have private, as you can see in the link of my example. how could I solve it please ?

    Thank you !

    #2024741
    Ying
    Staff
    Customer Support

    Hi Beatriz,

    1.This article should help for the column settings:
    https://docs.generatepress.com/article/option_generate_blog_settings/

    2.For the search result title, you can try this PHP snippet:

    function custom_search_result_title( $items) { 
       if ( is_search() ) { 
          $items = str_replace( "Search Results for", "Search custom Results for", $items ); 
       } 
       return $items; 
    } 
    
    add_filter( 'generate_search_title_output', 'custom_search_result_title', 9999, 2 ); 

    Replace the Search custom Results for with your custom title.

    3. GP has no control of the WP search output, here’s a snippet can be used on navigation search to customize the output of the navigation search:
    https://docs.generatepress.com/article/primary-navigation-layout-overview/

    If you only want to show the posts and pages in search result page, try this PHP snippet:

    add_filter( 'generate_navigation_search_output', function() {
        printf(  
            '<form method="get" class="search-form navigation-search" action="%1$s">
                <input type="search" class="search-field" value="%2$s" name="s" title="%3$s" />
                <input type="hidden" name="post_type" value="post" />
                <input type="hidden" name="post_type" value="page" />
            </form>', 
            esc_url( home_url( '/' ) ), 
            esc_attr( get_search_query() ),   
            esc_attr_x( 'Search', 'label', 'generatepress' ) 
        ); 
    } );

    Let me know if this helps ๐Ÿ™‚

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

    #2026798
    Beatriz

    Thank you for your reply Ying.
    1. super! it works! now I have to sort out the formatting a bit.

    2. I tried the code and it works but it doesn’t solve what I want to do. I don’t really want to change the title string. What I want is to present the title in a hero block. I tried it with the template content but what it shows, is the first article of my search result (as shown in my private link).

    3. I tried the code but I still do not see much change, what I see in my search result, is that the contents are presented very different from each other without me finding a possible relationship. Some results only show the title, others the title plus the image and others the title, the image, the text and the read more button, independently if they are articles or pages. (see the link). I don’t understand why.

    #2028074
    Ying
    Staff
    Customer Support

    1. Glad to hear that ๐Ÿ™‚

    2. Search result or blog page doesn’t have its own page title, so it will automatically use the first post’s title as its title. I don’t see a way to add the dynamic title into a block element at this moment Unfortuanly. You might need to use CSS to style the default search results for xsection.

    3. The reason that some of them show the excerpt some not is that some of the text nested in a container block which stops WP generate expert automatically (it’s a known Gutenberg bug). If you assign a block element – content template to the search result page, the excerpt nested in GB container might show.

    For some posts that don’t have thumbnails, it’s likely they don’t have a featured image assigned in the page editor.

    #2033524
    Beatriz

    Thank you Yin,
    2- The page that lists the search results, has a title, with a phrase beginning with “Rร‰SULTATS DE RECHERCHE POUR :” and ending with the search term. This is the title that I want to put in a header, but when I try to do it with a block, the title is taken from the first post that is found. So two H1 are generated, the title of the post and the title of the page. But it is not serious, I will create a header only with a static image.

    3 – I understand that some results do not show the image because there is no image. However, I have used the content template and there is still the same problem with the excerpt. I can see that some posts that belong to x category, are displayed and others are not. Or some pages that are child pages do not show the excerpt either.

    #2034260
    Ying
    Staff
    Customer Support

    In the content template, do you have a dynamic content block in it to show the excerpt?

    Let me know ๐Ÿ™‚

    #2035939
    Beatriz

    yes, it does, but it is present in some results and not in others.

    #2036240
    Ying
    Staff
    Customer Support

    I just tested, seems even using dynamic content block in content template can’t get around the WP bug.

    In that case, I would recommend using custom excerpt, or move the text out of the container block in the post.

    Let me know.

    #2042777
    Beatriz

    Hi Ying! sorry for the delay, indeed, when the text is inside a block, the excerpt does not appear.
    I can solve it with the articles but not with the content of my static pages.
    A simple option, for the moment, is to present only the title and the image, as a result of the search.
    Thank you !

    #2042859
    Ying
    Staff
    Customer Support

    You are welcome ๐Ÿ™‚

    I hope WP can get this bug fixed soon, it’s really annoying sometimes.

    #2235065
    Elvis

    Hello Ying,

    I am using a hero block element to design my search results title.
    So I want to remove the default one and since element checkbox does not do that I was looking arrund the forum to see if there is a solution.

    I have modified the function you provided to remove the title like so:

    function prefix_remove_default_search_results_title( $items ) { 
    	if ( is_search() ) { 
    		 $items = str_replace( "Rezultati pretrage za: %s", "", $items ); 
    	} 
    	return; 
    } 
    add_filter( 'generate_search_title_output', 'prefix_remove_default_search_results_title', 9999, 2 );

    It seems to be working, but I wanted to check if this seems ok to you too?
    Tnx

    #2235512
    Ying
    Staff
    Customer Support

    Hi Elvis,

    There’s actually a filter to remove the search result title:

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

    Sweet.
    I taught there had to be something like this … just was not able to find.
    Thanks Ying … awsome help.

    #2238290
    Ying
    Staff
    Customer Support

    You are welcome ๐Ÿ™‚

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