面包屑中的页面层次结构

时间:2015-10-14 23:54:17

标签: wordpress wordpress-plugin

我在Wordpress中创建一个网站并使用Yoast SEO插件。它存在并创建面包屑并在帖子的层次结构中完美地工作,但不在页面层次结构中。例如......

我有一个名为"第一页"第二页称为"第二页"这是"第一页的女儿"。

两页面包屑看起来像这样:

"Home > Page Two", 

但我需要它看起来像这样:

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

如何更改面包屑以按我需要的方式显示?即使在设置中,我也没有找到与之相关的任何内容。

感谢。

1 个答案:

答案 0 :(得分:1)

我在此文件 /wordpress-seo/frontend/class-breadcrumbs.php 中找到了一个非常有用的过滤器( wpseo_breadcrumb_links ),我希望能解决您的问题。< / p>

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