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

Php 如何更改我的帐户页面标题?,php,wordpress,woocommerce,hook-woocommerce,account,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

我想更改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_循环检查,否则这不起作用,这显然是不理想的,因为它最终也会改变页面上的其他内容

然后我发现,作为如何更改帐户详细信息页面标题的示例:

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;
}
但这不起作用,它甚至似乎根本没有进入函数


有没有办法做到这一点呢?

您可以通过以下方式更改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文件中。已测试且有效。

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

更改我的主帐户页标题和我的帐户页标题子部分:

1对于我的帐户:dashbord主页:

要更改“我的帐户”主页面标题,您只需直接在后端更改该页面的标题,因为它不是终结点,并在“设置”>“高级”部分中检查具有重命名标题的页面是否仍分配给“我的帐户”页面

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

使用,其中{$endpoint}需要被目标端点替换。请参见设置>高级部分中的可用端点

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

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文件。测试和工作

如果它不起作用,那是因为其他东西在制造麻烦,比如你自己的定制,你的主题的定制,插件或其他东西

有关答案:


更改我的主帐户页标题和我的帐户页标题子部分:

1对于我的帐户:dashbord主页:

要更改“我的帐户”主页面标题,您只需直接在后端更改该页面的标题,因为它不是终结点,并在“设置”>“高级”部分中检查具有重命名标题的页面是否仍分配给“我的帐户”页面

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

使用,其中{$endpoint}需要被目标端点替换。请参见设置>高级部分中的可用端点

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

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文件。测试和工作

如果它不起作用,那是因为其他东西在制造麻烦,比如你自己的定制,你的主题的定制,插件或其他东西

有关答案:


我注意到我在实现is_wc_endpoint_url&&in_循环解决方案时遇到了一些问题,它与&&in_循环不起作用,删除不是一个可行的选项,因为它会产生更多的冲突

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

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


我注意到我在实现is_wc_endpoint_url&&in_循环解决方案时遇到了一些问题,它与&&in_循环不起作用,删除不是一个可行的选项,因为它会产生更多的冲突

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

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


根据这一点,它不会更改条目标题。OP不想更改菜单项标题,但页面标题:@Brett尝试了吗?您发布的代码与我说的完全相同,但不起作用?@Brett您发布了woocommerce\u endpoint\u orders\u title钩子。我在用另一个钩子。。。所以这不是同一个钩子。但你必须知道这一点;根据这一点,它不会更改条目标题。OP不想更改菜单项标题,但页面标题:@Brett尝试了吗?您发布的代码与我说的完全相同,但不起作用吗?@Brett您已经发布了
发布了woocommerce\u endpoint\u orders\u标题钩子。我在用另一个钩子。。。所以这不是同一个钩子。但你必须知道这一点;出于某种原因,这对我不起作用;我必须进一步调查。@Brett很抱歉,如果您更改我的帐户页面的页面标题,它会更改标题,因为它是一个页面,所以dashbord的默认显示标题不是端点。购物车、结帐、条款和条件也是一样。…@Mr.Jo他想更改页面标题,而不是菜单项标签名称……我的答案可行,但他不行,我不知道为什么…@LoicTheAztec-Hmm,这就是他的问题。我们无法通过我们的魔法玻璃球看到他的孩子主题。@Brett我在我的页面上用函数“theu title”做了完全相同的事情。如果Loic或我的答案在你的页面上不起作用,那么你的页面肯定有严重的问题;我必须进一步调查。@Brett很抱歉,如果您更改我的帐户页面的页面标题,它会更改标题,因为它是一个页面,所以dashbord的默认显示标题不是端点。购物车、结帐、条款和条件也是一样。…@Mr.Jo他想更改页面标题,而不是菜单项标签名称……我的答案可行,但他不行,我不知道为什么…@LoicTheAztec-Hmm,这就是他的问题。我们无法通过我们的魔法玻璃球看到他的孩子主题。@Brett我在我的页面上用函数“theu title”做了完全相同的事情。如果Loic或我的答案在你的页面上不起作用,那么你的页面肯定有严重的问题。