首页 > 解决方案 > WordPress: Add page to Yoast SEO breadcrumbs

问题描述

I want to add an extra page to the breadcrumb trail of my site. The problem is, that I'm using WooCommerce and on the breadcrumb doesn't work right on the my account dashboard. It always shows the following trail:

Home > My account

Even if I'm on a child page like "edit account". These child pages aren't really pages. They are WooCommerce endpoints on the same page.

It should look like this:

Home > My account > Orders > Order ID

I tried to add a page but couldn't figure it out. To remove a page from the breadcrumb trail, I'm using the following code:

/**
 * Conditionally Override Yoast SEO Breadcrumb Trail
 * http://plugins.svn.wordpress.org/wordpress-seo/trunk/frontend/class-breadcrumbs.php
 * -----------------------------------------------------------------------------------
 */

add_filter( 'wpseo_breadcrumb_links', 'wpse_100012_override_yoast_breadcrumb_trail' );

function wpse_100012_override_yoast_breadcrumb_trail( $links ) {
    global $post;

    if ( is_home() || is_singular( 'post' ) || is_archive() ) {
        $breadcrumb[] = array(
            'url' => get_permalink( get_option( 'page_for_posts' ) ),
            'text' => 'Blog',
        );

        array_splice( $links, 1, -2, $breadcrumb );
    }

    return $links;
}

Code is from here: https://wordpress.stackexchange.com/a/121618/96806 I've already changed it to check if I'm on a WooCommerce endpoint.

This code works fine. But how can I change it to add a page instead of deleting it?

I guess it has something to do with the array_splice ;-)

标签: phpwordpresswoocommerceyoast

解决方案


OK, I have a solution.

Big thanks to the answer from @WebElaine: https://wordpress.stackexchange.com/a/332300/96806

Here's my full code to change the Yoast breadcrumb navigation in the WooCommerce My Account area:

add_filter('wpseo_breadcrumb_links', 'woocommerce_account_breadcrumb_trail');
function woocommerce_account_breadcrumb_trail($links) {
    if ( is_wc_endpoint_url() or is_account_page() ) {

        $endpoint       = WC()->query->get_current_endpoint();
        $endpoint_title = WC()->query->get_endpoint_title( $endpoint );
        $endpoint_url   = wc_get_endpoint_url($endpoint);

        if ( is_account_page() && !is_wc_endpoint_url() ) :
            //$links[2] = array('text' => $endpoint_title, 'url' => $endpoint_url, 'allow_html' => 1);

        elseif ( is_wc_endpoint_url( 'edit-account' ) ) :
            $links[2] = array('text' => $endpoint_title, 'url' => $endpoint_url, 'allow_html' => 1);

        elseif ( is_wc_endpoint_url( 'orders' ) ) :
            $links[2] = array('text' => $endpoint_title, 'url' => $endpoint_url, 'allow_html' => 1);

            elseif ( is_wc_endpoint_url( 'view-order' ) ) :
                $endpoint_orders        = 'orders';
                $endpoint_orders_title  = WC()->query->get_endpoint_title( $endpoint_orders );
                $endpoint_orders_url    = wc_get_endpoint_url($endpoint_orders);

                $links[2] = array('text' => $endpoint_orders_title, 'url' => $endpoint_orders_url, 'allow_html' => 1);
                $links[3] = array('text' => $endpoint_title, 'url' => $endpoint_url, 'allow_html' => 1);

        elseif ( is_wc_endpoint_url( 'edit-address' ) ) :
            $links[2] = array('text' => $endpoint_title, 'url' => $endpoint_url, 'allow_html' => 1);

        elseif ( is_wc_endpoint_url( 'payment-methods' ) ) :
            $links[2] = array('text' => $endpoint_title, 'url' => $endpoint_url, 'allow_html' => 1);

            elseif ( is_wc_endpoint_url( 'add-payment-method' ) ) :
                $endpoint_payment_methods       = 'payment-methods';
                $endpoint_payment_methods_title = WC()->query->get_endpoint_title( $endpoint_payment_methods );
                $endpoint_payment_methods_url   = wc_get_endpoint_url($endpoint_payment_methods);

                $links[2] = array('text' => $endpoint_payment_methods_title, 'url' => $endpoint_payment_methods_url, 'allow_html' => 1);
                $links[3] = array('text' => $endpoint_title, 'url' => $endpoint_url, 'allow_html' => 1);

        endif;

    }

    return $links;

}

I'm happy about every feedback.


推荐阅读