• I’d like to display the object of the search after “search results for:”, and also in the breadcrumbs “search results for…”

    The page I need help with: [log in to see the link]

Viewing 12 replies - 1 through 12 (of 12 total)
  • How do you add the text there? Your theme support should be able to help you the fastest if it’s not custom programming.

    Moderator bcworkz

    (@bcworkz)

    As long as the global $wp_query object had not been usurped for other purposes, you could do something like:
    echo 'search results for: ', esc_html( get_query_var('s'));

    Or if that does not work for some reason, then:
    echo 'search results for: ', esc_html( sanitize_text_field( $_GET['s']));

    Thread Starter sacconi

    (@sacconi)

    they doesnt work, but I already have a couple of functions for the search, in order to add a specific search for the posts code:

    function search_by_post_id($query) {
        if($query->is_search) {
            if(is_numeric($query->query_vars['s'])) {
                $query->set('post_type', 'any');
                $query->set('post__in', array((int)$query->query_vars['s']));
                $query->set('s', '');
            }
        }
        return $query;
    }
    add_filter('pre_get_posts', 'search_by_post_id');
    
    /**
    * Add search custom posts by their ID in WordPress dashboard
    *
    */
    add_action( 'parse_request', 'cdxn_search_by_id' );
    function cdxn_search_by_id( $wp ) {
        global $pagenow;
        if( !is_admin() && 'edit.php' != $pagenow && 'post' !== $_GET['post_type']) {
    		return;
    	}
            
        // If it's not a search return
        if( !isset( $wp->query_vars['s'] ) ) {
    		return;
    	}
            
        // Validate the numeric value
        $id = absint( substr( $wp->query_vars['s'], 0 ) );
        if( !$id ) {
    		return; 
    	}
            
        unset( $wp->query_vars['s'] );
        $wp->query_vars['p'] = $id;
    }
    

    Moderator bcworkz

    (@bcworkz)

    get_query_var('s') doesn’t work because you’ve $query->set('s', '').
    implode(',', get_query_var('post__in')) should work.
    $_GET['s'] should work as well.

    echo 'search results for: ', esc_html( sanitize_text_field( $_GET['s']));
    placed on your search results template doesn’t work? Could the output be hidden by CSS perhaps? Look in the source HTML. This code does work on my test site.

    Thread Starter sacconi

    (@sacconi)

    echo ‘search results for: ‘, I thought it was to be placed in functions.php file, now I inspected the search.php file, maybe here is it the place?

    	<?php
    					/* translators: %s: search query. */
    					printf( esc_html__( 'Search Results for: %s', 'sacconicase' ), '<span>' . get_search_query() . '</span>' );
    					?>
    Moderator bcworkz

    (@bcworkz)

    Yes, that looks like the place.

    FYI, get_search_query() is basically a wrapper function for get_query_var('s'). Since you’ve unset “s” earlier, it’d be ineffective as well. It could be replaced with esc_html( implode(',', get_query_var('post__in'))) or esc_html( sanitize_text_field( $_GET['s']))

    Thread Starter sacconi

    (@sacconi)

    I used the second and it works! the strange fact for me is the object of the search comes out in orange, the colour I set for the posts count in category pages, but it’s ok, I think in this way it is armonized with the other pages

    Moderator bcworkz

    (@bcworkz)

    Page title segments within span tags get colored orange.

    .page-title span {
        color: orange;
    }

    To have the search term be blue-green like the rest of the “Risultati di…” title you could remove the span tags. I agree though that leaving it orange mimics the site logo and fits well with the overall theme.

    Thread Starter sacconi

    (@sacconi)

    How could I display the object of the search also in the breadcrumbs? https://test.sacconicase.com/?s=8175

    Moderator bcworkz

    (@bcworkz)

    You’d use the same $_GET['s'] method as for the title. You’d need to find where the code that outputs Cerca risultati per '' to work it in.

    Does the breadcrumb trail come from your theme or a plugin? How you’d work it in differs between code you maintain or someone else’s.

    Thread Starter sacconi

    (@sacconi)

    it’s a plugin: Breadcrumb NavXT

    Moderator bcworkz

    (@bcworkz)

    You would need to find a filter or action hook in the plugin source code that’ll let you add the search term to breadcrumb output. If the plugin devs have not provided such a hook you may need to resort to inserting the term via JavaScript or maybe some dynamic CSS.

    If you need help locating an appropriate hook I suggest asking in the plugin’s dedicated support forum where it’s devs and expert users are in the best position to help you.

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘displaying the object of the search’ is closed to new replies.