使用 pre_get_posts 编辑影响 ACF 选项的 WooCommerce 商店循环
Using pre_get_posts to edit WooCommerce shop loop affecting ACF options
目前在我的 WooCommerce 商店页面上,所有 ACF 选项字段都没有出现,尽管它们在其他页面上出现了。这包括徽标、wp_menu 页脚导航和一些其他高级自定义字段。作为参考,请参见以下屏幕截图:
所有其他页面的页脚:ibb.co/4R79mRZ
WooCommerce 页面(商店)上的页脚:ibb.co/88sjgSd
所有其他页面上的 header:ibb.co/vwzPs6w
WooCommerce 页面(商店)上的 header:ibb.co/9NJ0y2v
我已经确定问题出在我的 functions.php 文件中,同时使用 pre_get_posts 根据查询字符串中的参数编辑 WooCommerce 商店循环。
当我删除这段代码时,问题出现了:
add_action('pre_get_posts', 'filter_pre_get_posts' );
function filter_pre_get_posts( $wp_query ) {
if(is_shop()){
if(isset($_GET['brand'])) {
$filter_term = $_GET['brand'];
}
if(isset($_GET['cat'])) {
$filter_term = $_GET['cat'];
}
if(isset($_GET['type'])) {
$type = $_GET['type'];
}
if(isset($_GET['priceone'])) {
$price = $_GET['priceone'];
}
if(isset($_GET['pricetwo'])) {
$price = $_GET['pricetwo'];
}
if ($filter_term && !$type && !$price) {
echo "hi";
$wp_query->set('tax_query', array(
'relation' => 'OR',
array(
'taxonomy' => 'pa_branding',
'field' => 'slug',
'terms' => $filter_term ,
'include_children' => true,
),
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $filter_term ,
'include_children' => true,
),
array(
'taxonomy' => 'product_tag',
'field' => 'slug',
'terms' => $filter_term ,
'include_children' => true,
)
));
}
}
}
有趣的是,当我在此代码的条件中回显 'hi' 时,front-end 上显示以下内容:
注意左上角的回显 "hi":https://ibb.co/3dPg4d9
注意页脚中几个地方的 echo'd "hi":https://ibb.co/Zfh8rJg
站点未通过 ACF 选项字段或 wp_menu.
显示的所有位置均出现回声
我猜这与全局循环有关,但我真的不知道。
如有任何帮助,我们将不胜感激。
谢谢。
已回答:
使用 woocommerce_product_query 而不是 pre_get_posts。
目前在我的 WooCommerce 商店页面上,所有 ACF 选项字段都没有出现,尽管它们在其他页面上出现了。这包括徽标、wp_menu 页脚导航和一些其他高级自定义字段。作为参考,请参见以下屏幕截图:
所有其他页面的页脚:ibb.co/4R79mRZ
WooCommerce 页面(商店)上的页脚:ibb.co/88sjgSd
所有其他页面上的 header:ibb.co/vwzPs6w
WooCommerce 页面(商店)上的 header:ibb.co/9NJ0y2v
我已经确定问题出在我的 functions.php 文件中,同时使用 pre_get_posts 根据查询字符串中的参数编辑 WooCommerce 商店循环。
当我删除这段代码时,问题出现了:
add_action('pre_get_posts', 'filter_pre_get_posts' );
function filter_pre_get_posts( $wp_query ) {
if(is_shop()){
if(isset($_GET['brand'])) {
$filter_term = $_GET['brand'];
}
if(isset($_GET['cat'])) {
$filter_term = $_GET['cat'];
}
if(isset($_GET['type'])) {
$type = $_GET['type'];
}
if(isset($_GET['priceone'])) {
$price = $_GET['priceone'];
}
if(isset($_GET['pricetwo'])) {
$price = $_GET['pricetwo'];
}
if ($filter_term && !$type && !$price) {
echo "hi";
$wp_query->set('tax_query', array(
'relation' => 'OR',
array(
'taxonomy' => 'pa_branding',
'field' => 'slug',
'terms' => $filter_term ,
'include_children' => true,
),
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $filter_term ,
'include_children' => true,
),
array(
'taxonomy' => 'product_tag',
'field' => 'slug',
'terms' => $filter_term ,
'include_children' => true,
)
));
}
}
}
有趣的是,当我在此代码的条件中回显 'hi' 时,front-end 上显示以下内容:
注意左上角的回显 "hi":https://ibb.co/3dPg4d9
注意页脚中几个地方的 echo'd "hi":https://ibb.co/Zfh8rJg
站点未通过 ACF 选项字段或 wp_menu.
显示的所有位置均出现回声我猜这与全局循环有关,但我真的不知道。
如有任何帮助,我们将不胜感激。
谢谢。
已回答:
使用 woocommerce_product_query 而不是 pre_get_posts。