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

时间:2019-02-27 21:59:54

标签: php wordpress woocommerce hook-woocommerce account

我想在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;
}

但这没用,它甚至似乎根本没有加入该功能。

有没有一种方法可以真正起作用?

3 个答案:

答案 0 :(得分:2)

更改主要的“我的帐户页面标题”和“我的帐户页面标题”子部分:

1)对于“我的帐户:dashbord (主页)

  

要更改主要的“我的帐户” 页面标题,您只需要直接更改后端的页面标题,因为它不是端点< / strong>,然后在Settings> Advanced部分中检查该页面(标题重命名)是否仍分配给“我的帐户页面”。

enter image description here


enter image description here


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

使用woocommerce_endpoint_{$endpoint}_title composite dedicated filter hook,其中{$endpoint}需要用目标端点代替(请参阅Settings> Advanced部分的可用端点) < / p>

示例:要更改我的帐户“订单”端点页面标题,您将使用:

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文件中。经过测试,可以正常工作。

enter image description here

  

如果它不起作用,那是因为其他问题正在困扰您,例如您自己的自定义,主题的自定义,插件或其他…


相关的StackOverFlow答案:

答案 1 :(得分:2)

我注意到我在实现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>

答案 2 :(得分:0)

您可以通过以下方式更改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文件中。经过测试,可以正常工作。