2015-10-14 55 views
0

我在Wordpress中創建一個網站,並使用Yoast SEO插件。它存在並創建麪包屑並完美地在帖子的層次結構中工作,但不在頁面層次結構中。例如...麪包屑中頁面的層次結構

我有一個名爲「第一頁」的頁面和第二頁「第二頁」,它是「第一頁」的女兒。

兩個頁面的麪包屑看起來是這樣的:

"Home > Page Two", 

,但我需要它看起來是這樣的:

"Home > Page One > Page Two"... 

如何更改麪包屑出現,我需要的方式嗎?即使在設置中,我也沒有找到與其相關的任何內容。

謝謝。

+0

第2頁的網址是什麼? – vrajesh

+0

第2頁的網址是「site/page-one/page-two」@vrajesh –

回答

1

我在這個文件/wordpress-seo/frontend/class-breadcrumbs.php發現了一個非常有用的過濾器(wpseo_breadcrumb_links),我希望能夠解決你的問題。

add_filter('wpseo_breadcrumb_links', 'wpseo_breadcrumb_links_fn'); 
function wpseo_breadcrumb_links_fn($links) { 
    global $post; 

    // apply only on pages 
    if (is_page()) { 
     $post_parent = $post->post_parent; 

     // get all parent posts 
     while ($post_parent != 0) { 
      $post_aux = get_post($post_parent); 
      $post_parents[] = array(
       'text' => $post_aux->post_title, 
       'url' => get_permalink($post_aux->ID) 
      ); 
      $post_parent = $post_aux->post_parent; 
     } 

     // reverse array items 
     $post_parents = count($post_parents) > 1 ? array_reverse($post_parents) : $post_parents; 

     // add $post_parents in $links 
     array_splice($links, 1, -2, $post_parents); 
    } 

    return $links; 
} 
+0

非常感謝,馬科斯!簡單的代碼,它的工作原理,完美! –