首页 > 解决方案 > 如果在页面中选中复选框,如何按分类获取帖子

问题描述

我为自定义帖子类型和具有相同值的页面注册了自定义分类,当 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

        )
    )
);

标签: phpwordpress

解决方案


1- 创建一个模板并将此模板分配给所有需要的具有这些分类法的页面

2-在此模板代码中,检测所选分类法(您已从管理面板页面编辑器中选择)

3-在模板代码中,获取所有具有这些分类的帖子

$args = array(
    'tax_query' => array(
        array(
            'taxonomy' => 'genre',
            'field' => 'slug',
            'terms' => 'jazz'
        )
    )
);
$postslist = get_posts( $args );

参考: https ://codex.wordpress.org/Template_Tags/get_posts


推荐阅读