2

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

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

类别结构:

  • 页脚
  • 侧边栏
  • 赞助商页面
    • 金子
    • 青铜

如果我访问 myurl.com/sponsors-page/,它将显示金、银和铜类别中的所有广告。到目前为止,一切都很好。但我希望它按子类别的顺序显示它们并回显子类别名称。例如:

  • 金子
    • 广告 1
    • 广告 2
    • 广告 3
    • 广告 4
  • 青铜
    • 广告 5
    • 广告 6

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

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

4

1 回答 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++;
            }
        }
    }
}
于 2013-03-27T22:05:22.763 回答