繁体   English   中英

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

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

我想更改 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检查,这显然是不理想的,然后它结束了在页面上改变其他的一些东西。

然后我找到了这个答案,例如如何更改“帐户详细信息”页面的标题:

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) 对于“我的帐户:dashbord (主页)

要更改主“我的帐户”页面标题,您只需要直接更改后端页面的标题,因为它不是端点,并在Settings > Advanced部分检查页面(具有重命名的标题)仍然被分配到“我的帐户页面”。

在此处输入图片说明


在此处输入图片说明


2) 对于“我的帐户”部分中的其他“端点”页面标题:

使用woocommerce_endpoint_{$endpoint}_title复合专用过滤器钩子,其中{$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文件中。 测试和工作。

我注意到我在实现is_wc_endpoint_url() && in_the_loop解决方案时遇到了一些问题,它没有与&& in_the_loop() ,删除不是一个可行的选择,因为它造成了更多的冲突。

所以我选择直接处理模板文件,我分享这个是因为我注意到有些人有同样的问题,这个解决方案可能会帮助其他人使用不同的方法实现类似的结果。

我基本上用以下代码替换了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