按类别将第一、第二、第三等职位分组

时间:2021-10-04 作者:Matthew Wright

这是一个奇怪的问题,也许是不可能的。

我有一个设计,其中每个类别在索引中都有特定的样式。所有内容都在一个网格中,因此一个类别占用两列,另一个类别占用两行,其余两个类别仅占用一行和一列。工作得很好,直到你考虑到客户可能在同一类别中连续发布3次。

我需要一种输出帖子的方法,这样无论其他帖子的发布日期如何,类别都始终位于网格中的同一位置。

<category 1 - most recent post>
<category 2 - most recent post>
<category 3 - most recent post>
<category 4 - most recent post>
<category 1 - 2nd most recent post>
<category 2 - 2nd most recent post>
<category 3 - 2nd most recent post>
<category 4 - 2nd most recent post>
等等。

欢迎提出任何想法。

UPDATE: per request for code tried. 我有一个笨拙的开始,那就是3个带偏移量的单独查询,但解决方案需要处理无限数量的帖子。我知道这不是一个好办法,但希望我面前的一点东西不那么抽象,能引出一个更好的想法。

<?php $cat_terms = get_terms(\'category\');
            foreach($cat_terms as $cat_term) {
                wp_reset_query();
                
                $catQuery = array(
                    \'post_type\' => \'post\',
                    \'posts_per_page\' => 1,
                    \'order\' => \'DESC\',
                    \'tax_query\' => array(
                        array(
                            \'taxonomy\' => \'category\',
                            \'field\' => \'slug\',
                            \'terms\' => $cat_term->slug,
                        ),
                    ),
                 );

                 $cat = new WP_Query($catQuery); ?>
                 <?php if($cat->have_posts()) {
                  echo \'<section class="">\';  
                    while($cat->have_posts()) : $cat->the_post(); ?>
            
                    <div id="post-<?php the_ID(); ?>" class="entry">
                        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
                        <?php the_excerpt(); ?>
                    </div>
                    
                  <?php endwhile; ?>
          
              </section>
      
                 <?php } } ?>
    
            <?php wp_reset_query(); ?>
            
            <?php $cat_terms = get_terms(\'category\');
            foreach($cat_terms as $cat_term) {
                wp_reset_query();
                
                $catQuery = array(
                    \'post_type\' => \'post\',
                    \'posts_per_page\' => 1,
                    \'offset\' => 1,
                    \'order\' => \'DESC\',
                    \'tax_query\' => array(
                        array(
                            \'taxonomy\' => \'category\',
                            \'field\' => \'slug\',
                            \'terms\' => $cat_term->slug,
                        ),
                    ),
                 );

                 $cat = new WP_Query($catQuery); ?>
                 <?php if($cat->have_posts()) {
                  echo \'<section class="">\';  
                    while($cat->have_posts()) : $cat->the_post(); ?>
            
                    <div id="post-<?php the_ID(); ?>" class="entry">
                        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
                        <?php the_excerpt(); ?>
                    </div>
                    
                  <?php endwhile; ?>
          
              </section>
      
                 <?php } } ?>
    
            <?php wp_reset_query(); ?>
            
            <?php $cat_terms = get_terms(\'category\');
            foreach($cat_terms as $cat_term) {
                wp_reset_query();
                
                $catQuery = array(
                    \'post_type\' => \'post\',
                    \'posts_per_page\' => 1,
                    \'offset\' => 2,
                    \'order\' => \'DESC\',
                    \'tax_query\' => array(
                        array(
                            \'taxonomy\' => \'category\',
                            \'field\' => \'slug\',
                            \'terms\' => $cat_term->slug,
                        ),
                    ),
                 );

                 $cat = new WP_Query($catQuery); ?>
                 <?php if($cat->have_posts()) {
                  echo \'<section class="">\';  
                    while($cat->have_posts()) : $cat->the_post(); ?>
            
                    <div id="post-<?php the_ID(); ?>" class="entry">
                        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
                        <?php the_excerpt(); ?>
                    </div>
                    
                  <?php endwhile; ?>
          
              </section>
      
                 <?php } } ?>
    
            <?php wp_reset_query(); ?>

1 个回复
SO网友:Matthew Wright

我仍在努力准确地确定whythis 邮递

<?php
$a1 = get_posts(array(
   \'post_type\' => \'post\',
   \'posts_per_page\' => \'-1\',
   \'tax_query\' => array(
       array(
           \'taxonomy\' => \'category\',
           \'field\' => \'slug\',
           \'terms\' => \'cat-1\'
       )
   ),
   \'fields\' => \'ids\' // only get post IDs.
));
$a2 = get_posts(array(
   \'post_type\' => \'post\',
   \'posts_per_page\' => \'-1\',
   \'tax_query\' => array(
       array(
           \'taxonomy\' => \'category\',
           \'field\' => \'slug\',
           \'terms\' => \'cat-2\'
       )
   ),
   \'fields\' => \'ids\' // only get post IDs.
));
$a3 = get_posts(array(
   \'post_type\' => \'post\',
   \'posts_per_page\' => \'-1\',
   \'tax_query\' => array(
       array(
           \'taxonomy\' => \'category\',
           \'field\' => \'slug\',
           \'terms\' => \'cat-3\'
       )
   ),
   \'fields\' => \'ids\' // only get post IDs.
));
$a4 = get_posts(array(
   \'post_type\' => \'post\',
   \'posts_per_page\' => \'-1\',
   \'tax_query\' => array(
       array(
           \'taxonomy\' => \'category\',
           \'field\' => \'slug\',
           \'terms\' => \'cat-4\'
       )
   ),
   \'fields\' => \'ids\' // only get post IDs.
));
$count = 999;
$newArray = array();
for ($i = 0; $i < $count; $i++) {
   $newArray[] = $a1[$i];
   $newArray[] = $a2[$i];
   $newArray[] = $a3[$i];
   $newArray[] = $a4[$i];
}


$wp_query = new WP_Query(array(
   \'post_type\' => \'post\',
   \'post__in\' => $newArray,
   \'orderby\' => \'post__in\'
));

if ($wp_query -> have_posts()):
   while ($wp_query -> have_posts()):
       the_post(); ?>

    <div id="post-<?php the_ID(); ?>" class="entry">
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php the_excerpt(); ?>
    </div>


   <?php endwhile;
endif;
?>