简体   繁体   English

用ACF自定义字段替换Woocommerce分类标准存档页面标题

[英]Replace Woocommerce taxonomy archive pages title with ACF custom field

I am trying to figure out how to replace "woocommerce_page_title" with an ACF field? 我想弄清楚如何用ACF字段替换“ woocommerce_page_title”? If there isn't one then it should fall back to "woocommerce_page_title();". 如果没有,则应退回到“ woocommerce_page_title();”。 I can't seem to find anything super helpful. 我似乎找不到任何超级有用的东西。 Your help would be greatly appreciated. 您的帮助将不胜感激。

add_filter( 'woocommerce_page_title', 'custom_title' );
function custom_title( $title ) {
    $title = get_field("custom_tag_h1");
    return $title;
}

For Archives pages taxonomy titles and ACF: 对于“档案”页面分类标题和ACF:

The woocommerce_page_title works for shop page and taxonomy archive pages, so in ACF: woocommerce_page_title适用于商店页面和分类归档页面,因此在ACF中:

在此处输入图片说明

Then in a product category for example (here "Clothing") , you set your custom title: 然后在一个产品类别(例如“服装”)中 ,设置自定义标题:

在此处输入图片说明

In the code, you need to get the queried object (the WP_Term object) for the taxonomy archive pages and to set it as following: 在代码中,您需要获取分类档案页面的查询对象WP_Term对象)并将其设置如下:

add_filter( 'woocommerce_page_title', 'custom_title' );
function custom_title( $page_title ) {
    if ( ! function_exists('get_field') || is_search() )
        return $page_title;

    if ( is_tax() ) {
        $term   = get_queried_object();
        $the_id = $term->taxonomy . '_' . $term->term_id;
    } elseif ( is_shop() ) {
        $the_id = wc_get_page_id( 'shop' );
    }
    return get_field( "custom_tag_h1", $the_id ) ? get_field( "custom_tag_h1", $the_id ) : $page_title;
}

Code goes in function.php file of your active child theme (or active theme). 代码进入您的活动子主题(或活动主题)的function.php文件中。 Tested and work. 经过测试和工作。

在此处输入图片说明

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

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