首页 文章

列出类别页面上的自定义帖子类型除以其子类别

提问于
浏览
2

在Wordpress中,如何在类别页面上列出自定义的帖子类型除以帖子所在的子类别?

我的例子:我有自定义分类广告 . 我还有一个名为广告的自定义帖子类型 .

分类结构:

  • 页脚

  • 补充工具栏

  • 赞助商页面

  • 青铜

如果我去myurl.com/sponsors-page/,它将显示金,银和铜类别的所有广告 . 到现在为止还挺好 . 但我希望它按子类别的顺序显示它们并回显子类别名称 . 例如:

  • 广告1

  • 广告2

  • 广告3

  • 广告4

  • 青铜

  • 广告5

  • 广告6

我该如何做到这一点?随意质疑我的方法,我是Wordpress的新手 .

我觉得这可能是重复的,但当我说我试图搜索时,请相信我 .

1 回答

  • 3

    我刚开始使用Wordpress,但这就是我想你想做的事情 .

    // get available taxonomies
    $taxonomies = get_object_taxonomies ( (object) array ('post_type' => 'subcategory' ));
    
    // loop all taxonomies
    foreach( $taxonomies as $taxonomy ) { 
    
        // Gets every "category" (term) in this taxonomy to get the respective posts
        $terms = get_terms( $taxonomy );
    
        // loop through the terms
        foreach( $terms AS $term ) {
            // get posts
            $posts = new WP_Query( "taxonomy=$taxonomy&term=$term->slug" );
    
            // check for posts
            if ( $posts-> have_posts() ) {
                // how your header (gold,silver,bronze)
                echo '<h2>' . $term-> name . '</h2>';               
    
                // loop through posts
                while ( $posts-> have_posts() ) {
                    // get the post
                    $posts-> the_post();
    
                    // show your ad
                    echo $posts-> post-> post_content;
    
                    // Update temporary value
                    $posts_count++;
                }
            }
        }
    }
    

相关问题