且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

WP SEO Yoast插件面包屑和JSON-LD(BreadcrumbList)集成

更新时间:2023-12-01 23:11:52

您可以过滤输出并收集JSON.但是,在下面提供的示例中,如果要在文档的开头部分中输出,则收集"可能会很晚.然后,您可以更早地调用面包屑函数,而无需回显它并收集数据,删除过滤器并呈现JSON.

You can filter the output and collect your JSON. However, in provided example below, the "collect" is maybe to late if you wanna output within the head section of the document. You can then call the breadcrumb function earlier, without echo it and collect the data, remove the filter and render the JSON.

/* echo breadcrumbs in template */
yoast_breadcrumb('<p id="breadcrumbs">','</p>');

/* collect breadcrumb whenever */
$breadcrumbs = yoast_breadcrumb('','',false);

这是过滤器功能:

add_filter('wpseo_breadcrumb_links', 'entex_add_crumb_schema', 10, 1);
function entex_add_crumb_schema($crumbs) {

    if( ! is_array( $crumbs ) || $crumbs === array()){
        return $crumbs;
    }

    $last = count($crumbs);
    $listItems = [];
    $j = 1;

    foreach ( $crumbs as $i => $crumb ) {

        $item = [];
        $nr = ($i + 1);

        if(isset($crumb['id'])){
            $item = [
                '@id' => get_permalink($crumb['id']),
                'name' => strip_tags( get_the_title( $id ) )
            ];
        }

        if(isset($crumb['term'])){
            $term = $crumb['term'];

            $item = [
                '@id' => get_term_link( $term ),
                'name' => $term->name
            ];
        }

        if(isset($crumb['ptarchive'])){
            $postType = get_post_type_object($crumb['ptarchive']);

            $item = [
                '@id' => get_post_type_archive_link($crumb['ptarchive']),
                'name' => $postType->label
            ];
        }

        /* READ NOTE BELOW: */

        if($nr == $last){
            if(is_author() && !isset($crumb['url'])) $crumb['url'] = esc_url(get_author_posts_url(get_queried_object_id()));
        }

        /* The 'text' indicates the current (last) or start-path crumb (home)*/
        if(isset($crumb['url'])) {
            if($crumb['text'] !== '') {
                $title = $crumb['text'];
            } else {
                $title = get_bloginfo('name');
            }

            $item = [
                '@id' => $crumb['url'],
                'name' => $title
            ];
        }

        $listItem = [
            '@type' => 'ListItem',
            'position' => $j,
            'item' => $item
        ];

        $listItems[] = $listItem;
        $j++;
    }

    $schema = [
        '@context' => 'http://schema.org',
        '@type' => 'BreadcrumbList',
        'itemListElement' => $listItems
    ];

    $html = '<script type="application/ld+json">' . stripslashes(json_encode($schema, JSON_PRETTY_PRINT)) . '</script> ';
    echo $html;
    remove_filter('wpseo_breadcrumb_links', 'entex_add_crumb_schema', 10, 1);
    return $crumbs;
}

(*)注意 Yoast不会将URL填充到当前登录页面/归档登录页面.您必须在函数中为作者档案添加示例.这取决于您是否要在架构中使用当前路径,因此我将在每个用户案例中对其进行修改.

(*) NOTE Yoast doesnt populate the urls to current landing-pages/ archive landing pages. You have to add those with the example for the author archive in the function. This depends if you want the current trail or not in the schema, so I leave this to be modified at each user case.

(*)提示 这是RAW示例.对您填充的var进行一些检查,以避免javascript范围问题.另外,仅当使用PRETTY参数时,才需要最后一个反斜杠.

(*) TIPS This is RAW examples. Do some sanitazion on your populated vars, to avoid javascript scope issues. Also the last stripslashes is only needed if you use PRETTY arguments.

快乐JSON