【问题标题】:Wordpress Category archive: Separate loops for separate post typesWordpress 类别存档:不同帖子类型的独立循环
【发布时间】:2015-12-11 22:14:47
【问题描述】:

我的网站有两种自定义帖子类型,但两者都使用相同的类别/标签。

我正在尝试创建一个 category.php 页面,该页面将允许我显示该类别中的所有项目,但在两个单独的区域中,每个区域一个。

我可以让第一个帖子类型显示在主循环中,但是如何构建第二个循环以仅显示属于该类别的第二个类型的帖子?

【问题讨论】:

    标签: wordpress categories custom-post-type archive


    【解决方案1】:

    我建议您使用 WP_Query 和 args 部分的实例,因为这两个部分都为所需的部分设置了 post_type=。

    <?php
    
    //general code to get the current category id
    
    $category = get_category( get_query_var( 'cat' ) );
    $cat_id = $category->cat_ID;
    
    $args = array(
        'post_type' => 'post',
        'posts_per_page' => 5,
        'cat' => $cat_id,
    
    );
    $the_query = new WP_Query( $args ); 
    if ( $the_query->have_posts() ) : 
    while ( $the_query->have_posts() ) : $the_query->the_post();
    
         echo get_the_title();
         echo get_the_excerpt();
    
    
        endwhile;
        wp_reset_postdata(); 
    
    
    
    //for second loop of second post type
    
    $args1 = array(
        'post_type' => 'your_post_type',
        'posts_per_page' => 5,
        'cat' => $cat_id,   
    );
    $the_query1 = new WP_Query( $args1 ); 
    if ( $the_query1->have_posts() ) : 
    while ( $the_query1->have_posts() ) : $the_query1->the_post();
    
         echo get_the_title();
         echo get_the_excerpt();
    
    
        endwhile;
        wp_reset_postdata(); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多