【问题标题】:List custom post types on a category page divided by its subcategories列出类别页面上的自定义帖子类型,按其子类别划分
【发布时间】:2013-03-25 20:47:39
【问题描述】:

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

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

类别结构:

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

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

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

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

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

【问题讨论】:

    标签: wordpress wordpress-theming


    【解决方案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++;
                }
            }
        }
    }
    

    【讨论】:

    • 干杯!当我有能力的时候会给你赏金。为了将来参考,我还在 get_terms 中添加了“array('parent' => get_queried_object()->term_id)”,这样我就只能得到当前术语的孩子。再次感谢您。
    • @JohanDavidsson 你是如何添加的?
    猜你喜欢
    • 1970-01-01
    • 2016-07-24
    • 1970-01-01
    • 1970-01-01
    • 2015-10-01
    • 2015-08-02
    • 2014-06-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多