WordPress Puzzles in Search Results

In WordPress, I am currently using Yoast SEO Plugin to display breadcrumbs for my pages and posts, which works great when visiting a specific page.

Here is the function I use to display breadcrumbs inside my WordPress templates:

<?php if ( function_exists('yoast_breadcrumb') ) { yoast_breadcrumb('<p id="breadcrumbs">','</p>'); } ?> 

For example, when viewing Team Members , which is a child of About Us , I get:

 Home > About Us > Team Members 

However, I would like to be able to display the same crackers (for individual pages and posts) inside the search results loop.

So far, while searching for Members , the following is displayed:

Your search results:

 Team Members Home > Search > Members Members Area Home > Search > Members 

But I don’t want breadcrumbs on the Search Results page, I want them to appear on separate pages and messages that appear as a result of a keyword search.

For example, imagine what I was looking for again for Members . I would like to display the following:

Your search results:

 Team Members Home > About Us > Team Members Members Area Home > Members Area 

I haven’t fussed if it is or is not integrated with the SEO plugin, but so far this is the best solution I have found for displaying breadcrumbs in WordPress!

It also requires an incase expression, here is my search.php file: http://pastebin.com/0qjb2954

+5
source share
5 answers

Try it. This is my own working snippet that shows crackers inside the search loop.

 /*Begin Loop */ <?php echo '<div class="b-search_result_list__item_breadcrumbs breadcrumbs">'; $current_type = get_post_type(); if ($current_type == 'page') { $parents = get_post_ancestors(get_the_ID()); if($parents){ for($i=count($parents)-1;$i>=0;$i--){ echo '<span typeof="v:Breadcrumb">'; echo '<a rel="v:url" property="v:title" title="'.get_the_title($parents[$i]).'" href="'.get_permalink($parents[$i]).'">'.get_the_title($parents[$i]).'</a>'; echo '</span>'; } }else{ echo '<span typeof="v:Breadcrumb">'; echo '<a rel="v:url" property="v:title" title="'.get_bloginfo('name').'" href="'.get_bloginfo('url').'">'.get_bloginfo('name').'</a>'; echo '</span>'; } echo '<span typeof="v:Breadcrumb">'; echo '<span property="v:title">'.get_the_title().'</span>'; echo '</span>'; }else{ $current_obj = get_post_type_object($current_type); echo '<span typeof="v:Breadcrumb">'; echo '<a rel="v:url" property="v:title" title="'.get_bloginfo('name').'" href="'.get_bloginfo('url').'">'.get_bloginfo('name').'</a>'; echo '</span>'; echo '<span typeof="v:Breadcrumb">'; echo '<a rel="v:url" property="v:title" title="'.$current_obj->labels->name.'" href="'.get_post_type_archive_link( $current_type ).'">'.$current_obj->labels->name.'</a>'; echo '</span>'; $current_taxonomies = get_object_taxonomies($current_type); if($current_taxonomies){ $current_terms = get_the_terms(get_the_ID(), $current_taxonomies[0]); if($current_terms){ $current_term = array_shift($current_terms); echo '<span typeof="v:Breadcrumb">'; echo '<a rel="v:url" property="v:title" title="'.$current_term->name.'" href="'.get_term_link($current_term).'">'.$current_term->name.'</a>'; echo '</span>'; /* var_dump($current_obj->labels->name); // Archive name var_dump(get_post_type_archive_link( $current_type )); // Archive link var_dump($current_term->name); // Term name var_dump(get_term_link($current_term)); // Term link var_dump(get_permalink()); // Post link */ } } echo '<span typeof="v:Breadcrumb">'; echo '<span property="v:title">'.get_the_title().'</span>'; echo '</span>'; } echo '</div>'; ?> /*End Loop*/ 
+2
source

Using a plugin to create breadcrumbs is really not required. Here's a simple PHP function that you can add to the functions.php file:

 function breadcrumbs() { global $post; echo "<ul id='breadcrumbs'>"; if (!is_home()) { echo '<li><a href="' . get_option('home') . '">Home</a></li>'; if (is_category() || is_single()) { echo "<li>" . the_category(' </li><li> '); if (is_single()) { echo "</li><li>" . the_title() . "</li>"; } } elseif (is_page()) { if($post->post_parent){ foreach ( get_post_ancestors( $post->ID ) as $ancestor ) { echo '<li><a href="' . get_permalink($ancestor) . '" title="' . get_the_title($ancestor) . '">' . get_the_title($ancestor) . '</a></li>' . get_the_title(); } } else { echo "<li>" . get_the_title() . "</li>"; } } } elseif (is_tag()) { single_tag_title(); } elseif (is_day()) { echo "<li>Archive for " . the_time('F jS, Y') . "</li>"; } elseif (is_month()) { echo "<li>Archive for " . the_time('F, Y') . "</li>"; } elseif (is_year()) { echo "<li>Archive for " . the_time('Y') . "</li>"; } elseif (is_author()) { echo "<li>Author Archive</li>"; } elseif (isset($_GET['paged']) && !empty($_GET['paged'])) { echo "<li>Blog Archives</li>"; } elseif (is_search()) { echo "<li>Search Results for" . the_search_query() . "</li>"; } echo "</ul>"; } 

along with some CSS for its style, customize as you wish

 #breadcrumbs { list-style:none; margin:5px 0; overflow:hidden; } #breadcrumbs li{ float:left; } #breadcrumbs li+li:before { content: '| '; padding:0 4px; } 

You can then implement these breadcrumbs on any page that you want, including the searchpage.php file or any file that you use to display search results with this call

 <?php breadcrumbs(); ?> 
+1
source

try adding this line of code above the yoast breadcrumb function to the search.php file:

 WPSEO_Breadcrumbs::$instance = NULL; 

This will be line 22, I believe, and I will definitely use the Yoast breadcrumb function from your question, and not the new breadcrumb () function that is now.

Please let me know if this works!

Full explanation:

The functionality of patches for Yoast plugins is based on page loading, based on the current page as a child. In order for it to load the correct child and parents, you will need to reset it before running the function. There is no built-in reset function, however, setting a static instance of $ to NULL should force the plugin to regenerate its data based on the current global post object, which is set during the loop.

0
source

Search pages have a conditional function that can be used. You can always apply this to loading crackers. Here is an example:

 if ( ! is_search() ) { if ( function_exists('yoast_breadcrumb') ) { yoast_breadcrumb('<p id="breadcrumbs">','</p>'); } } 

It depends on where you load the breadcrumbs, but it usually works if your theme is not unique.

0
source

Based on Javor's answer, I found a way. I banged my head about this for several hours. You can back up and restore the otuside cycle. There he is:

 global $wp_query; //backup $old_singular_value = $wp_query->is_singular; //change $wp_query->is_singular = true; //reset WPSEO_Breadcrumbs::$instance = NULL; //breadcrumbs if (function_exists('yoast_breadcrumb')){ yoast_breadcrumb('<p id="breadcrumbs">','</p>'); } //restore $wp_query->is_singular = $old_singular_value; 

It pushes the request to make it unique, so the updated breadcrumbs think this is not a search page, but only one message or page or whatever you display as search results.

0
source

Source: https://habr.com/ru/post/1204432/


All Articles