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

问题描述 投票:1回答:1

我想弄清楚如何用ACF字段替换“woocommerce_page_title”?如果没有,那么它应该回到“woocommerce_page_title();”。我似乎找不到任何超级有用的东西。非常感谢您的帮助。

add_filter( 'woocommerce_page_title', 'custom_title' );
function custom_title( $title ) {
    $title = get_field("custom_tag_h1");
    return $title;
}
php wordpress woocommerce advanced-custom-fields custom-taxonomy
1个回答
0
投票

对于档案页面分类标题和ACF:

woocommerce_page_title适用于商店页面和分类法归档页面,因此在ACF中:

enter image description here

然后在产品类别中(例如“服装”),您可以设置自定义标题:

enter image description here

在代码中,您需要获取分类存档页面的查询对象(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;
}

代码位于活动子主题(或活动主题)的function.php文件中。经过测试和工作。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.