Skip to:
Content
Pages
Categories
Search
Top
Bottom

Breadcrumbs on Search Results Screen

  • @uniquepete

    Participant

    I have a problem with the way breadcrumbs are being displayed on my search results screen.

    First of all, I have the following (amongst other things) in my functions.php file:

    
    /**
    Just a simple breadcrumb trail, but, importantly, don't double up when we're at the root
    **/
    function mycustom_breadcrumb_options() {
    	// Home - default = true
    	$args['include_home']    = false;
    	// Don't need breadcrumbs if we're at the root
    	if ( apply_filters( 'bbp_no_breadcrumb', is_front_page() ) ) {
    	// Forum root - default = true
    		$args['include_root']    = false;
    	// Current - default = true
    		$args['include_current'] = false;
    	}
    	return $args;
    }
    add_filter('bbp_before_get_breadcrumb_parse_args', 'mycustom_breadcrumb_options' );
    
    /**
    Search only the specified forum
    **/
    function my_bbp_filter_search_results( $r ){ 
        //Get the submitted forum ID
        $forum_id = sanitize_title_for_query( $_GET['bbp_search_forum_id'] );
        //If the forum ID exits, filter the query
        if( $forum_id && is_numeric( $forum_id ) ){
            $r['meta_query'] = array(
                array(
                    'key' => '_bbp_forum_id',
                    'value' => $forum_id,
                    'compare' => '=',
                )
            );  
        }
        return $r;
    }
    add_filter( 'bbp_after_has_search_results_parse_args' , 'my_bbp_filter_search_results' );
    

    I have a search box on the forum home page and on the index page for each individual forum. For general navigation around my forum the breadcrumbs are presented as one might expect and search results are confined to those relevant to the current forum (or all forums from the home page).

    The breadcrumbs on the Forum ‘home page’ show simply as Forums and on individual forum index pages as Forums > ForumName, where ‘ForumName’ is the name of the current forum, etc. A search request from the Forum home page returns results as expected, with the breadcrumbs displayed as Forums > Search > Search Results for 'search string'. However, while a search from one of the individual forum index pages returns the refined search results as expected, the breadcrumbs are always displayed as Forums > Search > Search Results for 'search string', they don’t include the name of the forum that has been searched (i.e. Forums > ForumName > Search > Search Results for 'search string'.

    I think I can see that the search is actually being applied to all forums, then refined by the relevant function in the functions.php file, so I think I can see why I might have to do a little extra work to get the relevant forum name displayed in the breadcrumb, but can anyone suggest how I might do this? It would seem that I should be able to add a little more code to my mycustom_breadcrumb_options() function, but what exactly…?

    Thanks for any assistance anyone can offer.
    (WordPress 5.4.1, bbPress 2.6.5, website: digitalconcepts.net.au/forum – Please note, this part of the website is ‘hidden’ from the home page, you need to navigate there via the above direct URL)

Viewing 7 replies - 1 through 7 (of 7 total)
  • @robin-w

    Moderator

    that was an interesting challenge, as the forum ID is not known when the breadcrumbs start to show, so I had to look up the forum_id from the first result.

    but this should work, just add it to your functions file

    add_filter ('bbp_breadcrumbs' , 'rew_breadcrumb_search' ) ;
    
    function rew_breadcrumb_search ($crumbs) {
    	//create a new array
    	$new_crumbs = array() ;
    	//find the forum from the first item in results
    	if ( bbp_has_search_results()) {
    		while ( bbp_search_results() ) : bbp_the_search_result(); 
    		$type = get_post_type() ;
    		if ($type == 'topic' ) $forum_id = bbp_get_topic_forum_id() ;
    		if ($type == 'reply' ) $forum_id = bbp_get_reply_forum_id() ;
    		if (!empty ($forum_id)) break ;
    		endwhile ;
    	}
    	//cycle through the crumbs until we find search
    	foreach ($crumbs as $crumb=>$data) {
    		if (strpos($data, 'search') !== false) {
    			//If the forum ID exits, add the breadcrumb
    			if( $forum_id && is_numeric( $forum_id ) ){
    				$new_crumbs[] = '<a href="' . esc_url( bbp_get_forum_permalink( $forum_id ) ) . '" class="bbp-breadcrumb-forum">' . bbp_get_forum_title($forum_id ) . '</a>';
    			}
    		}
    		$new_crumbs[] = $data ;
    	}
    return $new_crumbs ;
    }

    @uniquepete

    Participant

    Thanks Robin. That’s given me something to work with.

    As it stands, this will include the current forum in the breadcrumb if the search is executed from a specific forum and the result set is not empty. That is certainly one of the cases addressed.

    But if the search is executed from the main index page, and the result set is not empty, instead of the breadcrumb trail including just Forums > Search > etc., it will include the name of the the first forum that produced a search hit (and obviously not all, where there may be several hits in different forums), which is not really the intent.

    If the search set is empty, however, the name of the forum from which the search was conducted is still missing and I’m not sure I can work out how to get around that, because your code is using one of the search hits to work out what forum to add to the breadcrumb. If the search set is empty, how then can I work out where I’ve come from? I think I’m going to have to know this to even fix the first problem above.

    Is there a concept of something like a global variable here? Can we set the $forum_id variable any time we navigate to a particular forum and just call on that from where ever we are if needed?

    I think it will be the case of the empty search where this will come in most handy, because the reader can quickly flick back to the forum in question and search again, rather than having to go back to the main index page and navigate back to the forum they were working with if they, for example, have just mistyped a search string.

    Thanks again. I had no idea where to even begin…

    @robin-w

    Moderator

    in both your cases (main search and empty search) then yes you will have a problem.

    main search can be excluded by looping round the search results and excluding the forum if the search results are from more than one forum.

    Global wp_query is available, and lists the query that was executed, so in there you would find the

    ‘key’ => ‘_bbp_forum_id’,
    ‘value’ => $forum_id,
    ‘compare’ => ‘=’, ,

    part of the query, so you could use that with some nifty code to extract the forum number, but I’ll leave you to work out how to do that !!

    @uniquepete

    Participant

    Ahh! To the reader left as an exercise it is!

    Thanks, I’ll see how I go.

    @robin-w

    Moderator

    come back of you need further help

    @uniquepete

    Participant

    Working with your original solution, your subsequent advice, playing with code segments from relevant template files, and a bit of dumb luck, the following augmented version of your function seems to work:

    
    function rew_breadcrumb_search ($crumbs) {
    	//create a new array
    	$new_crumbs = array() ;
    	$first_pass = true;
    	$augment_breadcrumbs = true;
    	//find the forum from the first item in results
    	if ( bbp_has_search_results()) {
    		while ( bbp_search_results() ) : bbp_the_search_result(); 
    		$type = get_post_type() ;
    		if ($type == 'topic' ) $forum_id = bbp_get_topic_forum_id() ;
    		if ($type == 'reply' ) $forum_id = bbp_get_reply_forum_id() ;
    		if ( $first_pass ) {
    			$first_pass = false;
    			$last_forum_id = $forum_id;
    		} elseif ( $forum_id !== $last_forum_id ) {
    			$augment_breadcrumbs = false;
    		}
    //		if (!empty ($forum_id)) break ;
    		endwhile ;
    	}
    	//cycle through the crumbs until we find search
    	foreach ($crumbs as $crumb=>$data) {
    		//shouldn't really need to do this inside the loop, but the alternatives crashed	
    		if ( $augment_breadcrumbs ) {
    			if (strpos($data, 'search') !== false) {
    				//If the forum ID exits, add the breadcrumb
    				if ( $forum_id && is_numeric( $forum_id ) ){
    					//Use the Forum ID we found in the search result
    					$new_crumbs[] = '<a href="' . esc_url( bbp_get_forum_permalink( $forum_id ) ) . '" class="bbp-breadcrumb-forum">' . bbp_get_forum_title( $forum_id ) . '</a>';
    				} else {
    					//The search result was empty, so we need to get the Forum ID from the search request(?)
    					$forum_id = sanitize_title_for_query( $_GET['bbp_search_forum_id'] );
    					if ( $forum_id && is_numeric( $forum_id ) ){
    						$new_crumbs[] = '<a href="' . esc_url( bbp_get_forum_permalink( $forum_id ) ) . '" class="bbp-breadcrumb-forum">' . bbp_get_forum_title( $forum_id) . '</a>';
    					}
    				}
    			}
    		}
    			$new_crumbs[] = $data ;
    		}
    	return $new_crumbs ;
    }
    add_filter ('bbp_breadcrumbs' , 'rew_breadcrumb_search' ) ;
    

    It does seem a little complicated, but all my attempts to cut out bits that don’t immediately look necessary breaks things, so I have what I haveā€”but it does produce the desired outcome.

    Thanks for your help. It was very much appreciated.

    @robin-w

    Moderator

    hey if it works, then it’s good code !!

    servers are so fast nowadays, that a few cycles extra will make no difference to what is downloaded !

    well done on cracking it, it’s always a good feeling when it works !

Viewing 7 replies - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.
Skip to toolbar