Skip to:
Content
Pages
Categories
Search
Top
Bottom

Reply To: Breadcrumbs on Search Results Screen

@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.

Skip to toolbar