2014-10-10 68 views
5

在WordPress中,我目前使用Yoast's SEO Plugin爲我的網頁和帖子顯示breadcrumbs,在訪問特定頁面時工作正常。WordPress搜索結果中的麪包屑

這裏是我使用,以顯示我的WordPress模板裏面的麪包屑功能:

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

例如瀏覽到Team Members這是當About Us一個孩子,我得到:

Home > About Us > Team Members 

但是,我希望能夠在搜索結果循環內顯示相同的麪包屑(針對各個頁面和帖子)。

到目前爲止在搜索Members就是顯示:

搜索結果:

Team Members 
Home > Search > Members 

Members Area 
Home > Search > Members 

但我不想在搜索結果頁面上的麪包屑,我想他們作爲搜索關鍵字的結果顯示的單個頁面和帖子。

例如想象我再次搜索Members我想顯示在下面:

搜索結果:

Team Members 
Home > About Us > Team Members 

Members Area 
Home > Members Area 

我不會大驚小怪,如果這是或不是與SEO插件集成,但迄今爲止,它是我發現在WordPress中顯示麪包屑的最佳解決方案!

而且櫃面abody需要它,這是我的search.php文件:http://pastebin.com/0qjb2954

回答

1

使用插件生成麪包屑是不是真的有必要。這裏有一個簡單的PHP功能,您可以添加到您的functions.php文件:一些CSS樣式它,定製你的願望

#breadcrumbs { 
    list-style:none; 
    margin:5px 0; 
    overflow:hidden; 
} 

#breadcrumbs li{ 
    float:left; 
} 
#breadcrumbs li+li:before { 
    content: '| '; 
    padding:0 4px; 
} 

然後就可以實現在任何網頁上的麪包屑沿

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>"; 
} 

你喜歡的,包括你的searchpage.php文件或文件爲準您使用此調用

<?php breadcrumbs(); ?> 
+1

對不起,剛發現一些語法錯誤,但現在應該都很好。幾個星期沒有觸及php,所以讓我知道它是如何發生的,但應該工作得很好。 – davidcondrey 2014-10-13 09:01:29

+0

它的晚我能說什麼..大聲笑 – davidcondrey 2014-10-13 09:06:58

+0

從您的搜索結果中發佈代碼php文件 – davidcondrey 2014-10-13 09:12:44

0

嘗試加入這一行上面的代碼yoast麪包屑功能我來顯示搜索結果n您的search.php中的文件:

WPSEO_Breadcrumbs::$instance = NULL; 

這將是22行,我相信,也請務必使用Yoast麪包屑功能,從你的問題,而不是新的麪包屑()函數,現在的存在。

請讓我知道這是否行得通!

充分說明:

的Yoast插件麪包屑的功能是建立在頁面加載,基於當前頁爲孩子上。爲了使它載入正確的孩子和父母,您需要在運行該功能之前將其重置。沒有內置的重置函數,但是將靜態$實例設置爲NULL應該會導致插件基於當前循環中設置的全局後對象重新生成其數據。

+0

@Giles你測試過了嗎? – Yavor 2014-10-16 17:33:17

+1

看起來這是不可能的。即使在重置實例之後,插件使用is_singular()來確定當前頁面是否爲帖子,並且該函數不能被覆蓋。因此,即使它在重置實例後加載正確的帖子,它也會根據當前頁面的類型(post/archive/category/etc)選擇錯誤類型的breadcrumb進行顯示。 – Yavor 2014-10-19 04:07:52

0

搜索頁面有conditional function可以使用。您始終可以將其應用於加載麪包屑。這裏有一個例子:

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

這取決於你在哪裏加載的麪包屑爲好,但這通常應該工作,除非你的主題非常獨特。

2

試試這個。這是我自己的工作片段,它顯示搜索循環內的麪包屑。

/*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*/ 
0

建立在Yavor的答案我找到了一種方法。幾個小時以來,我一直在喋喋不休。您可以放置​​備份並恢復循環的otuside。那就是:

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; 

它假貨這樣新刷新的麪包屑認爲這是不是搜索網頁,但單篇文章或頁面或任何你正在顯示的搜索結果的查詢,使之奇。