首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WordPress:向Yoast面包屑添加页面

WordPress:向Yoast面包屑添加页面
EN

Stack Overflow用户
提问于 2020-02-29 16:30:41
回答 1查看 508关注 0票数 0

我想添加一个额外的页面到我的网站的面包屑痕迹。问题是,我使用的是WooCommerce和面包屑,在我的帐户仪表板上不能正常工作。它总是显示以下线索:

主页>我的账户

即使我在像“编辑帐户”这样的子页面上。这些子页面并不是真正的页面。它们是同一页面上的WooCommerce端点。

它应该是这样的:

主页>我的帐户>订单>订单ID

我试着加了一页,但搞不懂。为了从面包屑轨迹中删除一个页面,我使用了以下代码:

代码语言:javascript
复制
/**
 * 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;
}

代码来自这里:https://wordpress.stackexchange.com/a/121618/96806,我已经更改了它,以检查我是否在WooCommerce端点上。

这个代码很好用。但是,如何更改它以添加一个页面而不是删除它呢?

我想这与array_splice有关;-)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-02-29 16:44:52

好吧,我有个解决办法。

感谢@WebElaine:https://wordpress.stackexchange.com/a/332300/96806的回答

下面是我在WooCommerce my区域中更改Yoast面包屑导航的完整代码:

代码语言:javascript
复制
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;

}

每一次反馈我都很高兴。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60467303

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档