【问题标题】:Multiple wordpress loops for custom post type & taxonomy with no duplicates用于自定义帖子类型和分类的多个 wordpress 循环,没有重复
【发布时间】:2014-01-09 13:25:27
【问题描述】:

我正在尝试制作一个自定义模板来显示来自相同自定义帖子类型但不同类别的多个循环。

这就是我所追求的:

来自自定义帖子类型:“作品集”

在自定义类别 1“音乐”中:

  • 顶部有 1 个精选帖子
  • 音乐标题
  • 3 个子特色帖子
  • 12 个帖子(仅标题)

在自定义类别 2“演讲者”中: - 主持人标题 - 3 个帖子

在自定义类别 3“新闻”中: - 新闻标题 - 3 个帖子

这是我正在使用的代码:

    <?php if (have_posts()) : while (have_posts()) : the_post(); //WP loop ?>
         <?php the_content(); ?>
            <?php $args=array( //Loop 1
                'post_type' => 'dt_portfolio',
                'taxonomy' => 'dt_portfolio_category',
                'term' => 'music',
                'posts_per_page' => 16
                );
                $myloop = new WP_Query($args);
                if($myloop->have_posts()) : while($myloop->have_posts()) :
                $myloop->the_post();
                 ?>

                      <!--the content -->

              <?php endwhile; endif; ?>
              <?php wp_reset_query(); // end music loop ?>
            <h2>Presenters</h2>
            <?php $args=array( //Loop 2
                'post_type' => 'dt_portfolio', 
                'taxonomy' => 'dt_portfolio_category',
                'term' => 'presenters',
                'posts_per_page' => 3
                );
                $myloop = new WP_Query($args);
                if($myloop->have_posts()) : while($myloop->have_posts()) :
                $myloop->the_post();
                 ?>

                      <!--the content -->

              <?php endwhile; endif; ?>
              <?php wp_reset_query(); // end presenters loop ?>
            <h2>News</h2>
            <?php $args=array( //Loop 3
                'post_type' => 'dt_portfolio',
                'taxonomy' => 'dt_portfolio_category',
                'term' => 'news',
                'posts_per_page' => 3
                );
                $myloop = new WP_Query($args);
                if($myloop->have_posts()) : while($myloop->have_posts()) :
                $myloop->the_post();
                 ?>

                      <!--the content -->

              <?php endwhile; endif; ?>
              <?php wp_reset_query(); // end news loop ?>

       <?php endwhile; endif; // end WP loop?>

总体而言,这 3 个循环运行良好。

我需要帮助的部分是第一个循环部分。我需要从同一个自定义分类“dt_portfolio_category”->“音乐”中获取所有 16 个帖子。但是将它们分成 1 个顶部精选帖子(全角),然后是标题,然后是 3 个子精选帖子(3 列),然后是 12 个只有标题的帖子(3 列)。我试图将它分成 3 个单独的循环,但是内容被重复了......我认为必须有一种更清洁的方法来做到这一点。

谢谢!

【问题讨论】:

  • 作为评论:你有一个父“循环”,里面有 3 个子“循环”。如果不正确,这是无用的。剪掉父“循环”。更重要的是,您对 WP_query 和 wp_reset_query() 有点困惑; 1)您实例化了3个同名的WP_Query对象,这不是错误,但会使代码更难阅读;而不是 $myloop 使用 $music$presenters$news 例如 2) wp_reset_query() 重置全局 $post 和相关的主查询,但它不会重置从 WP_Query 类实例化的对象:它们不'不需要重置。
  • 谢谢,可以稍微清理一下代码。

标签: wordpress loops custom-post-type custom-taxonomy no-duplicates


【解决方案1】:

老兄解决这个问题:

$args=array( //Loop 3
            'post_type' => 'dt_portfolio',
            'tax_query' => array(
                   array('taxonomy'=>'dt_portfolio_category',
                             'term'=> 'news',
                             'field'=>'slug')
            ),
            'posts_per_page' => 3
            );

剩下的是你的代码......没有变化。 希望它会工作

【讨论】:

    【解决方案2】:
    <?php
    $args=array(
        'post_type' => 'dt_portfolio',
        'taxonomy' => 'dt_portfolio_category',
        'term' => 'music',
        'posts_per_page' => 16
    );
    
    $music = new WP_Query($args);
    
    $counter = 1;
    
    if($music->have_posts()) : while($music->have_posts()) :
        $music->the_post();
    
        if ($counter = 1){
            # code...
            # I'd use some helper functions here
            print_main_music();
        } elseif ($counter > 1 && $counter < 5) {
            # code...
            # I'd use some helper functions here
            print_featured_music();
        } else {
            # code...
            # I'd use some helper functions here
            print_other_music();
        }
    
        $counter++;
    
    endwhile; endif;
    ?>
    

    辅助函数必须在您的functions.php 文件中,并且它们必须在您的 HTML 中加上模板标签(the_content()、the_title() 等...);我想你不是在要求整个 HTML+CSS 布局,对吧?

    显然你可以把 HTML 和 PHP 混合在一起......这不是很好,但为了测试目的它还可以。

    【讨论】:

    • 我似乎无法让它工作。它似乎没有增加计数器。
    • 对,我不是要整个 HTML + CSS。
    【解决方案3】:

    设计参数略有变化。我想出了一个正在努力展示的解决方案:

    1 个全角新闻

    3 条新闻摘录

    1 个全宽音乐项目

    16 个带有图片和标题的音乐项目

    来自其他类别的 3 个帖子

    来自不同杂项类别的 3 个帖子

    对于我正在使用的每个部分中的内容-get_template_part。

    这是有效的:

    从一个循环开始以显示第一个全角新闻:

    <?php
    $args=array(
        'post_type' => 'dt_portfolio',
        'taxonomy' => 'dt_portfolio_category',
        'term' => 'news',
        'posts_per_page' => 1                
    );
    
    $fullnewsloop = new WP_Query($args);
    
    if($fullnewsloop->have_posts()) : while($fullnewsloop->have_posts()) :
        $fullnewsloop->the_post();
    
        get_template_part( 'content-full-width', get_post_format() );
    endwhile; endif; ?>
    

    使用第二个循环显示接下来的 3 个新闻项目。偏移量是跳过fullnewsloop中已经显示的第一个新闻的关键。

    <?php
    $args=array(
        'post_type' => 'dt_portfolio',
        'taxonomy' => 'dt_portfolio_category',
        'term' => 'news',
        'posts_per_page' => 3,
        'offset' => 1 // this skips the first post from the news category.
    );
    
    $shortnewsloop = new WP_Query($args);
    
    if($shortnewsloop->have_posts()) : while($shortnewsloop->have_posts()) :
        $shortnewsloop->the_post();                
    
        get_template_part( 'content-title-excerpt', get_post_format() );
    endwhile; endif; ?>
    

    下一节使用不同的分类术语重复上述循环。

    <?php
    $args=array ( 
        'post_type' => 'dt_portfolio',
        'taxonomy' => 'dt_portfolio_category',
        'term' => 'music',
        'posts_per_page' => 1
    );
    
    $fullmusicloop = new WP_Query($args);
    
    if($fullmusicloop->have_posts()) : while($fullmusicloop->have_posts()) :
        $fullmusicloop->the_post();
    
        get_template_part( 'content-full-width', get_post_format() );
    endwhile; endif; ?>
    
    <?php
    $args=array(
        'post_type' => 'dt_portfolio',
        'taxonomy' => 'dt_portfolio_category',
        'term' => 'music',
        'posts_per_page' => 16,
        'offset' => 1 // this skips the post already displayed in the fullmusicloop.
    );
    
    $shortmusicloop = new WP_Query($args);
    
    if($shortmusicloop->have_posts()) : while($shortmusicloop->have_posts()) :
        $shortmusicloop->the_post();
    
        get_template_part( 'content-title-image', get_post_format() );
    endwhile; endif; ?>
    

    最后一部分是分类术语的另外两个循环。

    <?php
    $args=array(
        'post_type' => 'dt_portfolio',
        'taxonomy' => 'dt_portfolio_category',
        'term' => 'speakerss',
        'posts_per_page' => 3,
    );
    
    $speakersloop = new WP_Query($args);
    
    if($speakersloop->have_posts()) : while($speakersloop->have_posts()) :
        $speakersloop->the_post();                
    
        get_template_part( 'content-title-image', get_post_format() ); 
    endwhile; endif; ?>
    
    <?php
    $args=array(
        'post_type' => 'dt_portfolio',
        'taxonomy' => 'dt_portfolio_category',
        'term' => 'artists',
        'posts_per_page' => 3,
    );
    
    $artistsloop = new WP_Query($args);
    
    if($artistsloop->have_posts()) : while($artistsloop->have_posts()) :
        $artistsloop->the_post();
    
        get_template_part( 'content-title-image', get_post_format() );
    endwhile; endif; ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-03
      • 2021-04-19
      • 1970-01-01
      • 2015-08-12
      • 2013-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多