php - WordPress:如何防止使用functions.php显示子类别的帖子?
问题描述
如何仅显示父类别下的帖子但隐藏子类别下的帖子?
谢谢!
解决方案
将此代码放在functions.php下
add_action('pre_get_posts', 'filter_out_children');
function filter_out_children( $query ) {
if ( is_main_query() && is_category() && ! is_admin() ) {
$qo = $query->get_queried_object();
$tax_query = array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $qo->term_id,
'include_children' => false
);
$query->set( 'tax_query', array($tax_query) );
}
}
推荐阅读
- json - 未处理的异常:类型“int”不是 Flutter 应用程序中“String”类型的子类型
- html-table - 如何在 Rmarkdown 中制作图标和文本表格
- python - 在 Path 上安装 Python 3.9 时安装 Tensorflow?
- bootstrap-4 - 2 列布局,居中对齐,同时保持列背景颜色全尺寸
- r - 为每个前面的变量拟合一个交互项
- android - Android 模拟器不工作 Android Studio
- r - 将图像插入 gt 表
- python - 泊松函数与直方图数据的曲线拟合和
- snakemake - 运行多个snakemake规则
- ffmpeg - -b 标志未在 FFMpeg 中解释