php - 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
;-)
解决方案
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.
推荐阅读
- vue.js - 我在使用 npm 或 yarn 构建 vue.js 项目时遇到问题
- android - 在 github 操作工作流中下载操作存储库“r0adkll/sign-android-release@v1”时出错
- comments - 我的 Stata do 文件中有多少行代码,不包括注释?
- c - 如何正确遍历命令行参数以检查每个字符是否是 C 中的数字?
- c# - 将类中的属性列表与字符串列表匹配
- javascript - 每当有人从计算机中选择文件时,如何自动将图像重新缩放为 12 x 12
- python - 使用 Python 的 librosa 遍历音频文件
- ios - 如何通过 Stripe 直接向用户付款?
- angular - 通过复选框执行发送操作并在 Angular 9 中阻止它
- android - 调试模式和发布模式下的 Flutter 构建版本代码不一样