繁体   English   中英

WooCommerce:覆盖我帐户页面中“浏览器”选项卡上设置的标题

[英]WooCommerce: Override the title set to the browsers tab in my account page

我目前正在尝试在WooCommerce中访问“ My Account页面时更改在浏览器选项卡中设置的标题。

例如,当我进入“ Orders页面时,该选项卡仍被命名为“ My Account ,这并不是很好。 它应始终具有端点的名称/ account_menu_items 我在这里尝试过此操作,但这仅更改了左上角菜单内容上的标题:

/**
 * Change title for menu items
 */
add_filter( 'the_title', 'custom_account_endpoint_titles' );
function custom_account_endpoint_titles( $title ) {
    global $wp_query;

    if ( in_the_loop() && isset( $wp_query->query_vars['orders'] ) ) {
        return 'Orders';
    }

    return $title;
}

截图:

在此处输入图片说明

尝试改用pre_get_document_title过滤器,因为它允许您在呈现之前对其进行修改。

注意, $title_pieces实际上是一个看起来像

array (
  'title' => 'title example',
  'tagline' => 'just another wordpress blog'
}

所以你需要这样

add_filter( 'pre_get_document_title', 'custom_account_endpoint_titles' );

function custom_account_endpoint_titles($title_pieces) {

    global $wp_query;

    if ( in_the_loop() && isset( $wp_query->query_vars['orders'] ) ) {

      $title_pieces['title'] = 'Orders';
      //$title_pieces['tagline'] = 'Your tag line'; Set a tag line if you want to

      return $title_pieces;
    }

    return $title_pieces;

}

另外,请确保转储$wp_query->query_vars['orders']的值,以确保其值为您实际要查找的值

我终于找到了过滤器及其更改方法:

/**
 * Override woocommerce account endpoint titles in the browser tab
 */
add_filter( 'wpseo_title', 'woocommerce_endpoint_titles' );
function woocommerce_endpoint_titles( $title ) {
    $sep       = ' – ';
    $sitetitle = get_bloginfo();

    if ( is_wc_endpoint_url( 'orders' ) ) {
        $title = 'Bestellungen' . $sep . $sitetitle;
    } elseif ( is_wc_endpoint_url( 'view-order' ) ) {
        $title = 'Bestellung' . $sep . $sitetitle;
    }

    return $title;
}

如果您在WooCommerce中创建了自己的端点,则也可以使用此过滤器,但是您需要先注册自定义端点。 您可以这样操作:

/**
 * Add custom menu items to wc query vars so that we can use is_wc_endpoint_url()
 */
add_filter( 'woocommerce_get_query_vars', 'add_items_to_query_vars' );
function add_items_to_query_vars( $vars ) {

    foreach ( [ 'custom-endpoint-1', 'custom-endpoint-2' ] as $e ) {
        $vars[ $e ] = $e;
    }

    return $vars;
}

我希望这可以帮助别人。

暂无
暂无

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

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