简体   繁体   English

如何更改 WooCommerce 我的帐户页面标题?

[英]How to change WooCommerce My Account page titles?

I want to change the page titles (entry titles) for each page in the WooCommerce My Account section so that they relate to the actual page you are on rather than outputting a generic "My Account" on each page.我想更改 WooCommerce 我的帐户部分中每个页面的页面标题(条目标题),以便它们与您所在的实际页面相关,而不是在每个页面上输出通用的“我的帐户”。

I have looked around and seen this solution in several places:我环顾四周,在几个地方看到了这个解决方案:

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 );

This does not work unless you remove the in_the_loop check, which obviously isn't ideal as then it ends up changing other things on the page too.除非你删除这不起作用in_the_loop检查,这显然是不理想的,然后它结束了在页面上改变其他的一些东西。

Then I found this answer , as an example on how to change the title for the "Account Details" page:然后我找到了这个答案,例如如何更改“帐户详细信息”页面的标题:

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;
}

But this didn't work, it didn't even seem to go into the function at all.但这不起作用,它甚至似乎根本没有进入该功能。

Is there a way to do this that actually works?有没有办法做到这一点,实际上有效?

Changing main my account page title and My account page title sub sections:更改主我的帐户页面标题和我的帐户页面标题子部分:

1) For "My Account: dashbord (main page) : 1) 对于“我的帐户:dashbord (主页)

To change the main "My account" page title, you just need to change the title of the page in backend directly because it's not an endpoint , and to check in Settings > Advanced section that the page (with the renamed title) is still assigned to "My account page".要更改主“我的帐户”页面标题,您只需要直接更改后端页面的标题,因为它不是端点,并在Settings > Advanced部分检查页面(具有重命名的标题)仍然被分配到“我的帐户页面”。

在此处输入图片说明


在此处输入图片说明


2) For the other "endpoints" page titles in My account section: 2) 对于“我的帐户”部分中的其他“端点”页面标题:

Use woocommerce_endpoint_{$endpoint}_title composite dedicated filter hook , where {$endpoint} need to be replaced by the targeted endpoint (see available endpoints on Settings > Advanced section)使用woocommerce_endpoint_{$endpoint}_title复合专用过滤器钩子,其中{$endpoint}需要替换为目标端点(请参阅Settings > Advanced部分上的可用端点)

Example: To change My account "orders" endpoint page title you will use:示例:要更改我的帐户“订单”端点页面标题,您将使用:

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;
}

Code goes in function.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 function.php 文件中。 Tested and works.测试和工作。

在此处输入图片说明

If it doesn't work, it's because something else is making trouble, like your own customizations, your theme's customizations, a plugin or something else…如果它不起作用,那是因为其他东西在制造麻烦,例如您自己的自定义、主题的自定义、插件或其他东西……


Related StackOverFlow answers:相关的 StackOverFlow 答案:

You can change you MyAccount item titles this way:您可以通过以下方式更改 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;
}

To change the title on each account page as well, you need to add this too:要更改每个帐户页面上的标题,您还需要添加以下内容:

/**
 * 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;
}

If you're using Yoast SEO, you need to add another function to set the correct page titles in the browser tab.如果您使用 Yoast SEO,则需要添加另一个功能来在浏览器选项卡中设置正确的页面标题。 If you also need this, I'll expand my answer.如果你也需要这个,我会扩大我的答案。

Put this into you functions.php file.把它放到你的functions.php文件中。 Tested and works.测试和工作。

I notice I had some issues implementing the is_wc_endpoint_url() && in_the_loop solution, it did not work out with the && in_the_loop() , removing is not a viable option since it created even more conflicts.我注意到我在实现is_wc_endpoint_url() && in_the_loop解决方案时遇到了一些问题,它没有与&& in_the_loop() ,删除不是一个可行的选择,因为它造成了更多的冲突。

So instead I chose to work on the template file directly , I am sharing this because I noticed that some people had the same issues and this solution might help others achieve the similar results using a different approach.所以我选择直接处理模板文件,我分享这个是因为我注意到有些人有同样的问题,这个解决方案可能会帮助其他人使用不同的方法实现类似的结果。

I basically replaced the header in page-my-account.php with this code:我基本上用以下代码替换了page-my-account.php的标题

<header>
    <?php
        $main_title = get_the_title();
        
        $endpoint = WC()->query->get_current_endpoint();  
        $endpoint_title = __( WC()->query->get_endpoint_title( $endpoint ), 'text-domain' );  
        
        if ( $endpoint_title ){
            //If the endpoint exists use itself as the new custom title
            $custom_title = $endpoint_title;
        }
        else{
            // Here we can define the custom title for each endpoint we can use home_url() or strpos + add_query_arg()
            global $wp;
            if( basename( home_url($wp->request) ) == 'points-and-rewards' || ( strpos( (add_query_arg( $wp->query_vars)) , 'points-and-rewards' ) !== false ) ){
                $custom_title = __( 'Points and rewards', 'text-domain' );
            }
            elseif( basename(home_url($wp->request)) == 'payment-methods' ){
                $custom_title = __( 'Payment methods', 'text-domain' );
            }
            elseif( basename(home_url($wp->request)) == 'my-account' ){
                $custom_title = __( 'Dashboard', 'text-domain' );
            }
            //Add more custom titles here
            
        }

        if ( !empty( $custom_title ) ){
            //If there is a custom title
            $new_endpoint_title =  sprintf( '<h2>%s > %s</h2>', $main_title, $custom_title );
        }
        else{
            //If there is no custom title default to get_title()
            $new_endpoint_title = sprintf( '<h2>%s</h2>' , $main_title );
        }
    ?> 
    
    <?php //Echo's the resulting title ?>
    <?php echo $new_endpoint_title; ?>

</header>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM