問題描述
function woocommerce_output_related_products() {
$args = array(
'posts_per_page' => 4,
'columns' => 4,
'orderby' => 'rand', // @codingStandardsIgnoreLine.
'post__not_in' => array(502,281)
);
woocommerce_related_products( apply_filters( 'woocommerce_output_related_products_args', $args ) );
}
我將此函數從 includes/wc-template-functions.php
復制到我的主題的functions.php
I copied this function from includes/wc-template-functions.php
into my theme's functions.php
為了驗證我的更改是否有效,我將 posts_per_page
更改為 3,它只查詢 3 而不是 4.
To verify that my changes would work I changed the posts_per_page
to 3 and it queried only 3 instead of 4.
我需要排除一些產品,但 post__not_in
不起作用.
I need to exclude a few products, but post__not_in
is not working.
我做錯了嗎?我還能如何排除使用此功能的產品?
Am I doing something wrong? How else can I exclude products using this function?
我用這個函數輸出產品:woocommerce_output_related_products();
I'm outputting the products with this function: woocommerce_output_related_products();
好討厭的問題.我根本無法從這里排除產品.有人可以幫忙嗎?
such an obnoxious problem. I simply cannot exclude products from here. can anyone help?
我也試過這個:
add_filter( 'woocommerce_output_related_products_args', function( $args ) {
$args = wp_parse_args( array( "post__not_in" => array('502','281') ), $args );
return $args;
});
我做了 print_r($args) 并且它顯示我的post__not_in"被添加了,但產品仍然存在.我有正確的身份證.
i did print_r($args) and it showed that my "post__not_in" was being added, but the products are still there. I have the right ID.
推薦答案
改用 woocommerce_related_products
過濾器鉤子,這樣:
Use the woocommerce_related_products
filter hook instead, this way:
add_filter( 'woocommerce_related_products', 'exclude_related_products', 10, 3 );
function exclude_related_products( $related_posts, $product_id, $args ){
// HERE set your product IDs to exclude
$exclude_ids = array('502','281');
return array_diff( $related_posts, $exclude_ids );
}
代碼位于活動子主題(或活動主題)的 function.php 文件中.經測試有效.
這篇關于在 Woocommerce 中排除相關產品 ID的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!