最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

woocommerce offtopic - Changing Title Tag on Shop Archive Page (current solution reverting to Title of First Product in Loop)

programmeradmin2浏览0评论

Firstly, add_theme_support( 'title-tag' ); is enabled in my theme.

I have two filters to modify titles, this one hooking on to the_title

add_filter( 'the_title', 'custom_account_endpoint_titles', 10, 2 );
function custom_account_endpoint_titles( $title ) {

// Currently only have conditions here for My Account pages (is_account_page())
// example: if ( isset( $wp_query->query_vars['edit-account'] ) && is_account_page() ) {    

return $title;
}

Secondly I have another function which fixed some title issues on Account pages as well (if I do not add this, the My Account pages all revert to a title tag of simply My Account instead of the conditional statements set for the Title changed intended.

function theme_slug_filter_wp_title( $title_parts ) {
        $title_parts['title'] = get_the_title();
    return $title_parts;
}

add_filter( 'document_title_parts', 'theme_slug_filter_wp_title' );

Doing a little debugging -- adding a print_r on $title_parts in the document_title_parts function

<pre>Array
(
    [title] => Products
    [site] => Example
)
</pre>

One would think, that the Title on my Shop Archive page would just be Products, but that is not the case.

Doing the same print_r debugging on the the_title filter on my shop archive page...

brings back <pre>925 Sterling Silver.....</pre> (which is the name of the first product in the loop

followed by <pre>Shop</pre> for the breadcrumb

and then additionally above all my products in the shop archive, the title and a list of classes, but te html seems to be malformed when doing so which is odd:

<div class="grid_col">    <div <pre="">925 Sterling Silver...... class="shop-product hover-border nt-post-class masonry-item excerpt-none product type-product "

The only solution I have read which does not work is hooking onto the is_shop() function in my the_title filter, such as..

add_filter( 'the_title', 'custom_account_endpoint_titles', 10, 2 );
function custom_account_endpoint_titles( $title ) {
    global $wp_query;
    global $current_user;

    if (is_shop()) {
        return 'My Shop Archive';
    }

This does indeed change the <title> tag on the shop page to My Shop Archive... but then proceeds to change the title of all my products on the page to My Shop Archive!

What is the solution for just simply changing the <title> tag on my shops archive page without affecting the product titles like in the image above?

Firstly, add_theme_support( 'title-tag' ); is enabled in my theme.

I have two filters to modify titles, this one hooking on to the_title

add_filter( 'the_title', 'custom_account_endpoint_titles', 10, 2 );
function custom_account_endpoint_titles( $title ) {

// Currently only have conditions here for My Account pages (is_account_page())
// example: if ( isset( $wp_query->query_vars['edit-account'] ) && is_account_page() ) {    

return $title;
}

Secondly I have another function which fixed some title issues on Account pages as well (if I do not add this, the My Account pages all revert to a title tag of simply My Account instead of the conditional statements set for the Title changed intended.

function theme_slug_filter_wp_title( $title_parts ) {
        $title_parts['title'] = get_the_title();
    return $title_parts;
}

add_filter( 'document_title_parts', 'theme_slug_filter_wp_title' );

Doing a little debugging -- adding a print_r on $title_parts in the document_title_parts function

<pre>Array
(
    [title] => Products
    [site] => Example
)
</pre>

One would think, that the Title on my Shop Archive page would just be Products, but that is not the case.

Doing the same print_r debugging on the the_title filter on my shop archive page...

brings back <pre>925 Sterling Silver.....</pre> (which is the name of the first product in the loop

followed by <pre>Shop</pre> for the breadcrumb

and then additionally above all my products in the shop archive, the title and a list of classes, but te html seems to be malformed when doing so which is odd:

<div class="grid_col">    <div <pre="">925 Sterling Silver...... class="shop-product hover-border nt-post-class masonry-item excerpt-none product type-product "

The only solution I have read which does not work is hooking onto the is_shop() function in my the_title filter, such as..

add_filter( 'the_title', 'custom_account_endpoint_titles', 10, 2 );
function custom_account_endpoint_titles( $title ) {
    global $wp_query;
    global $current_user;

    if (is_shop()) {
        return 'My Shop Archive';
    }

This does indeed change the <title> tag on the shop page to My Shop Archive... but then proceeds to change the title of all my products on the page to My Shop Archive!

What is the solution for just simply changing the <title> tag on my shops archive page without affecting the product titles like in the image above?

Share Improve this question asked Nov 6, 2020 at 19:56 bbrumanbbruman 4841 gold badge5 silver badges16 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Seems like the solution I was looking for was !in_the_loop()

Full:

add_filter( 'the_title', 'custom_account_endpoint_titles', 10, 2 );
function custom_account_endpoint_titles( $title ) {
if (is_shop() && !in_the_loop() && ! is_admin() && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX )) {
    return 'Shop - Parure.co';
}
}

Note: the !is_admin and !DOING_AJAX functions are to prevent this from affecting product titles in the admin panel

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论