php - 我想使用 Post Id 从商店页面隐藏 woocommerce 产品
问题描述
我正在尝试使用 Post Id's 从商店页面隐藏 woo-commerce 产品。有许多不同的方法可以从商店页面隐藏,例如product_cat等。
我想通过传递特定的产品 id/post id 来隐藏商店页面中的特定产品!
解决方案
尝试以下操作:使用“pre_get_posts”钩子
<?php
add_action( 'pre_get_posts', 'erginous_exclude_id' );
function custom_pre_get_posts_query( $q ) {
if ( ! $q->is_main_query() ) return;
if ( ! $q->is_post_type_archive() ) return;
if ( ! is_admin() && is_shop() ) {
$q->set( 'post__not_in', array(70, 53) ); // Replace 70 and 53 with your products IDs. Separate each ID with a comma.
}
remove_action( 'pre_get_posts', 'erginous_exclude_id' );
}
推荐阅读
- c# - Xamarin.Forms iOS 依赖服务中的触觉反馈崩溃
- sql - SQL - 尝试按每个位数据类型更改进行分组
- customization - 如何在到期日自动延长 maximo 合同?
- javascript - 如何在 html (pug) 文件中使用 axios?
- javascript - ReactJS:使用按钮执行 POST 和 GET 请求
- flutter - 菜单绘制不断返回两个应用栏。怎么只退一个?
- maven - Cucumber Cluecumber 报告 - POM:在 POM 文件中传递命令行,而不是在终端中输入
- python - 寻找最近邻=,TypeError:只有整数标量数组可以转换为标量索引
- javascript - 如何根据浏览器窗口的宽度动态设置表格宽度
- neo4j - 为什么这个 neo4j 密码查询无法重写?