根据子自定义分类法显示帖子

问题描述 投票:0回答:1

我有一个名为电影的自定义CPT,它带有一个名为“电影城”的自定义分类,我们可以将城市和地区添加为子女。我希望以一种城市应该展示的方式展示或循环电影,然后是它的孩子“区”,然后是城市的那些小猫下的所有电影院。在附图中你可以看到我想要达到的粗略想法; see this image

任何帮助将非常感激。

问候,

Mueed。

wordpress custom-wordpress-pages
1个回答
0
投票

像这样的东西?

get_terms函数参数可以在这里找到:https://developer.wordpress.org/reference/classes/wp_term_query/__construct/

get_posts函数参数可以在这里找到:https://codex.wordpress.org/Template_Tags/get_posts

// Get top level cities
$cities = get_terms(array(
    'taxonomy' => 'cinema-city'
));

// Loop cities
foreach($cities as $city){

    // Output city html

    // Get all child districts by using 'child_of' argument
    $districts = get_terms(array(
        'taxonomy' => 'cinema-city',
        'child_of' => $city->term_id
    ));

    // Loop the child districts
    foreach($districts as $district){

    // Output distruct output

        // Get all posts of thad 
        $cinemas = get_posts(array(
            'post_type' => 'cinema',
            'tax_query' => array(
                'taxonomy' => 'cinema-city',
                'field' => 'id',
                'terms' => $district->term_id
            )
        ));

        // Loop over the cinemas
        foreach($cinemas as $cinema){

            // Output cinema output

        }

    }

}
© www.soinside.com 2019 - 2024. All rights reserved.