我想更改WooCommerce我的帐户部分中每个页面的页面标题(条目标题),以便它们与您所在的实际页面相关,而不是在每个页面上输出通用的“我的帐户”。
我环顾四周,在几个地方看到了这个解决方案:
function wpb_woo_endpoint_title( $title, $id ) {
if ( is_wc_endpoint_url( 'downloads' ) && in_the_loop() ) { // add your endpoint urls
$title = "Download MP3s"; // change your entry-title
}
elseif ( is_wc_endpoint_url( 'orders' ) && in_the_loop() ) {
$title = "My Orders";
}
elseif ( is_wc_endpoint_url( 'edit-account' ) && in_the_loop() ) {
$title = "Change My Details";
}
return $title;
}
add_filter( 'the_title', 'wpb_woo_endpoint_title', 10, 2 );
这不起作用,除非你删除in_the_loop
检查,这显然是不理想的,因为它最终也改变了页面上的其他东西。
然后我找到了this answer,作为如何更改“帐户详细信息”页面标题的示例:
add_filter( 'woocommerce_endpoint_edit-account_title', 'change_my_account_edit_account_title', 10, 2 );
function change_my_account_edit_account_title( $title, $endpoint ) {
$title = __( "Edit your account details", "woocommerce" );
return $title;
}
但是这没有用,它似乎根本没有进入功能。
有没有办法做到这一点,实际上有效?
更改主要我的帐户页面标题和我的帐户页面标题子部分:
1)对于“我的帐户:仪表板(主页):
要更改主“我的帐户”页面标题,您只需要直接更改后端页面的标题,因为它不是端点,并检查
Settings
>Advanced
部分是否仍然分配了页面(带有重命名的标题)到“我的帐户页面”。
2)对于“我的帐户”部分中的其他“端点”页面标题:
使用woocommerce_endpoint_{$endpoint}_title
composite dedicated filter hook,其中{$endpoint}
需要被目标端点替换(参见Settings
> Advanced
部分的可用端点)
示例:要更改我的帐户“订单”端点页面标题,您将使用:
add_filter( 'woocommerce_endpoint_orders_title', 'change_my_account_orders_title', 10, 2 );
function change_my_account_orders_title( $title, $endpoint ) {
$title = __( "Your orders", "woocommerce" );
return $title;
}
代码位于活动子主题(或活动主题)的function.php文件中。经过测试和工作。
如果它不起作用,那是因为其他东西正在制造麻烦,比如您自己的自定义,主题的自定义,插件或其他东西......
相关的StackOverFlow答案:
您可以通过以下方式更改MyAccount项目标题:
/**
* Rename WooCommerce MyAccount menu items
*/
add_filter( 'woocommerce_account_menu_items', 'rename_menu_items' );
function rename_menu_items( $items ) {
$items['downloads'] = 'Download MP3s';
$items['orders'] = 'My Orders';
$items['edit-account'] = 'Change My Details';
return $items;
}
要同时更改每个帐户页面上的标题,您还需要添加以下内容:
/**
* Change page titles
*/
add_filter( 'the_title', 'custom_account_endpoint_titles' );
function custom_account_endpoint_titles( $title ) {
global $wp_query;
if ( isset( $wp_query->query_vars['downloads'] ) && in_the_loop() ) {
return 'Download MP3s';
}
if ( isset( $wp_query->query_vars['orders'] ) && in_the_loop() ) {
return 'My Orders';
}
if ( isset( $wp_query->query_vars['edit-account'] ) && in_the_loop() ) {
return 'Change My Details';
}
return $title;
}
如果您正在使用Yoast SEO,则需要在浏览器选项卡中添加另一个功能来设置正确的页面标题。如果你还需要这个,我会扩展我的答案。
把它放到你的functions.php
文件中。经过测试和工作。