php - 如果在页面中选中复选框,如何按分类获取帖子
问题描述
我为自定义帖子类型和具有相同值的页面注册了自定义分类,当 Pges 中的分类选中复选框时,如何获取帖子。
这在变量中获取术语
$category = get_terms([
'taxonomy' => 'category',
'hide_empty' => false,
]);
$category = get_terms([
'taxonomy' => 'category',
'hide_empty' => false,
]);
$property_area = get_terms([
'taxonomy' => 'property_area',
'hide_empty' => false,
]);
$property_type = get_terms([
'taxonomy' => 'property_type',
'hide_empty' => false,
]);
WordPress 查询
$rent_properties = array(
'post_type' => 'properties',
'posts_per_page' => 20,
'post_status' => 'publish',
'paged' => $paged,
'tax_query' => array(
'relation'=>'AND',
array(
'taxonomy'=>'category',
'field' => 'field_id',
'terms' => $category,
),
array(
'taxonomy'=>'property_area',
'field' => 'fild_id',
'terms' =>$property_area,
),
array(
'taxonomy'=>'property_type',
'field' => 'slug',
'terms' => $property_type
)
)
);
解决方案
1- 创建一个模板并将此模板分配给所有需要的具有这些分类法的页面
2-在此模板代码中,检测所选分类法(您已从管理面板页面编辑器中选择)
3-在模板代码中,获取所有具有这些分类的帖子
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'genre',
'field' => 'slug',
'terms' => 'jazz'
)
)
);
$postslist = get_posts( $args );
推荐阅读
- javascript - 在单独的文件中简化 Express-Validator 中间件的任何方法
- excel - ="" 不会删除附加了公式的行。Excel
- python - 在 python 上使用递归功能创建帕斯卡三角形
- swiftui - SwiftUI - 列表项宽度不一致
- javascript - 如何从使用 Javascript 在 Chrome 中打开的 PDF 中获取当前页码?
- google-kubernetes-engine - 无法向 Anthos 注册外部 k8s 集群 - “PermissionDenied”
- mongoose - Model 类如何成为 Document 类的子类?
- html - 宽屏中的 CSS div 宽度问题
- algorithm - 合并未排序的集合
- docker - 如何获取 Docker Compose/Docker Swarm/Kubernetes 容器号