php Bootstrap 4 - Yoast SEO Breadcrumb - Google Microdata / Schema Markup&Structured Data

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php Bootstrap 4 - Yoast SEO Breadcrumb - Google Microdata / Schema Markup&Structured Data相关的知识,希望对你有一定的参考价值。

<?php
/**
 * Bootstrap 4 - Yoast SEO Breadcrumb - Google Microdata/Schema Markup & Structured Data
 * Passes Google Structured Data Testing Tool & HTML5 Validator (Valid HTML5 + ARIA + SVG 1.1 + MathML 2.0.)
 *
 * https://developers.google.com/search/docs/data-types/breadcrumbs
 * https://search.google.com/structured-data/testing-tool
 * https://html5.validator.nu/
 */


/**
 * Step 1: Implement Yoast SEO breadcrumbs
 * https://kb.yoast.com/kb/implement-wordpress-seo-breadcrumbs/
 * 
 * ------------------------------------------------------------
 *  
 * Step 2: Insert into footer.php or wherever you please.
 * Replace your_text_domain
 */

if (function_exists('your_text_domain_breadcrumb')) {
    your_text_domain_breadcrumb();
}

/**
 * Step 3: Insert into functions.php
 * Replace your_text_domain
 */
if ( function_exists( 'yoast_breadcrumb' ) ) {

    function your_text_domain_wpseo_breadcrumb_single_link( $output, $link ) {
        global $your_text_domain_breadcrumbs;
        if ( null === $your_text_domain_breadcrumbs ) {
            $your_text_domain_breadcrumbs = array();
        }
        $your_text_domain_breadcrumbs[] = $link;

        return $output;
    }

    function your_text_domain_wpseo_breadcrumb_output( $output ) {
        global $your_text_domain_breadcrumbs;
        if ( null !== $your_text_domain_breadcrumbs && is_array( $your_text_domain_breadcrumbs ) ) {
            $output = '<nav><ol class="breadcrumb" itemscope itemtype="http://schema.org/BreadcrumbList">';
            $cnt    = count( $your_text_domain_breadcrumbs );
            $i      = 0;
            foreach ( $your_text_domain_breadcrumbs as $link ) {
                $i ++;
                $url = ( $cnt !== $i ) && ( isset( $link['url'] ) && ( is_string( $link['url'] ) && $link['url'] !== '' ) ) ? $link['url'] : false;

                if ( isset( $link['text'] ) && ( is_string( $link['text'] ) && $link['text'] !== '' ) ) {
                    $css_classes = array( 'breadcrumb-item', );
                    if ( $i === $cnt ) {
                        $css_classes[] = 'active';
                    }
                    $wrapper_attributes             = array();
                    $css_class                      = preg_replace( '/\s+/', ' ', implode( ' ', array_filter( $css_classes ) ) );
                    $wrapper_attributes[]           = 'class="' . esc_attr( trim( $css_class ) ) . '"';
                    $wrapper_attributes[]           = 'itemprop="' . esc_attr( 'itemListElement' ) . '"';
                    $wrapper_attributes[]           = 'itemscope itemtype="' . esc_attr( 'http://schema.org/ListItem' ) . '"';
                    $output                         .= '<li ' . implode( ' ', $wrapper_attributes ) . '>';
                    $wrapper_attributes_item_type   = array();
                    $wrapper_attributes_item_type[] = 'itemscope itemtype="' . esc_attr( 'http://schema.org/Thing' ) . '"';
                    $wrapper_attributes_item_type[] = 'itemprop="' . esc_attr( 'item' ) . '"';

                    if ( $url ) {
                        $wrapper_attributes_item_type[] = 'href="' . esc_url( __( $url, 'your_text_domain' ) ) . '"';
                        $output                         .= '<a ' . implode( ' ', $wrapper_attributes_item_type ) . '>';

                    } else {
                        $output .= '<span ' . implode( ' ', $wrapper_attributes_item_type ) . '>';
                    }

                    $output .= '<span itemprop="' . esc_attr( 'name' ) . '">' . $link['text'] . '</span>';

                    $output .= $url ? '</a>' : '</span>';

                    $output .= '<meta itemprop="' . esc_attr( 'position' ) . '" content="' . esc_attr( $i ) . '">';
                    $output .= '</li>';
                }
            }

            $output .= '</ol></nav>';
        }

        return $output;
    }

    function your_text_domain_breadcrumb() {
        add_filter( 'wpseo_breadcrumb_single_link', 'your_text_domain_wpseo_breadcrumb_single_link', 1, 2 );
        add_filter( 'wpseo_breadcrumb_output', 'your_text_domain_wpseo_breadcrumb_output', 1 );

        return yoast_breadcrumb();
    }
}

以上是关于php Bootstrap 4 - Yoast SEO Breadcrumb - Google Microdata / Schema Markup&Structured Data的主要内容,如果未能解决你的问题,请参考以下文章

php 将Yoast SEO优先级更改为“低”以在Yoast metabox之前获得ACF字段。

php FIX-yoast-智能issue.php

php 改变Yoast SEO默认图像大小

php 禁用Yoast SEO主要类别功能

php 禁用Yoast SEO主要类别功能

php Yoast Breadcrumbs