首页 文章

面包屑中的页面层次结构

提问于
浏览
0

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

我有一个名为“第一页”的页面和一个名为“第二页”的第二页,它是“第一页”的女儿 .

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

"Home > Page Two",

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

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

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

谢谢 .

1 回答

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

相关问题