我想弄清楚如何用 ACF 字段替换“woocommerce_page_title”?如果没有,那么它应该回退到“woocommerce_page_title();”。我似乎找不到任何 super 有用的东西。我们将非常感谢您的帮助。
add_filter( 'woocommerce_page_title', 'custom_title' );
function custom_title( $title ) {
$title = get_field("custom_tag_h1");
return $title;
}
最佳答案
对于文件页面分类标题和 ACF:
woocommerce_page_title
适用于商店页面和分类存档页面,因此在 ACF 中:
然后在产品类别中,例如(此处为“服装”),您可以设置自定义标题:
在代码中,您需要获取分类存档页面的查询对象(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 文件中。经过测试并可以工作。
关于php - 将 Woocommerce 分类归档页面标题替换为 ACF 自定义字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53578989/