个性化阅读
专注于IT技术分析

在类别页面上按其子类别划分列出自定义帖子类型

点击下载

在Wordpress中, 如何在类别页面上列出自定义文章类型, 再按文章所属的子类别列出?

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

类别结构:

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

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

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

我该怎么做?随时质疑我的方法, 我是Wordpress的新手。

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


#1


我也刚开始使用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++;
            }
        }
    }
}
赞(0)
未经允许不得转载:srcmini » 在类别页面上按其子类别划分列出自定义帖子类型

评论 抢沙发

评论前必须登录!