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

时间:2013-03-25 20:47:40

标签: wordpress wordpress-theming

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

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

类别结构:

  • 页脚
  • 边栏
  • 赞助商页面
    • 青铜

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

    • Ad 1
    • Ad 2
    • Ad 3
    • Ad 4
  • 青铜
    • Ad 5
    • Ad 6

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

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

1 个答案:

答案 0 :(得分: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++;
            }
        }
    }
}
相关问题