如何对Foreach循环中的所有帖子进行计数并将其分成四份

时间:2020-01-30 作者:semory

我有一个循环:

<?php
$categories = get_terms(\'my_category\');
foreach ( $categories as $category ) :
?>
我想将帖子拆分为4个单独的div列,以便帖子标题按字母顺序垂直排列,如下所示:

a | d | g | j
b | e | h | k
c | f | i | l
你如何得到总的帖子数并将其平均分为四分之一?

编辑:这是我正在处理的整个循环

<?php
$categories = get_terms(\'archive_category\');
foreach ( $categories as $category ) :
?>
<div>
<a data-toggle="collapse" href="#<?php echo $category->slug; ?>"><h3><?php echo $count; ?><?php echo $category->name; ?></h3></a>
<div class="collapse" id="<?php echo $category->slug; ?>">
<?php
$posts = get_posts(array(
\'post_type\'  => array(\'custom_post\', \'custom_post\', \'custom_post\'),
\'taxonomy\' => $category->taxonomy,
\'term\'  => $category->slug,
\'numberposts\' => -1,
\'orderby\'=>\'title\',
\'order\'=>\'ASC\'
));

foreach($posts as $post) :
setup_postdata($post); 
?>

<a href="<?php the_permalink(); ?>"><?php echo get_the_title( $p->ID ); ?></a>

<?php endforeach; ?>
</div>
</div>
<?php endforeach; ?>

2 个回复
最合适的回答,由SO网友:Antti Koskinen 整理而成

几年前我也有类似的问题。这是我发表的一个要点,它展示了我如何解决这个问题的基本想法,https://gist.github.com/koskinenaa/4d8461116885c0e64d5ca167ae53434d

将要点应用到你的情况中,我认为解决方案可能是这样的。我没有测试这个,所以它可能需要一些调整,但它至少应该为您指出正确的方向。当然,您需要为ul 元素将它们放置在彼此相邻的位置。

在我的示例中,我将术语和post查询移动到它们自己的助手函数,但这只是一个偏好问题。

function archive_categories( string $tax ) {
  $categories = get_terms($tax);
  return ( $categories && is_array($categories) ) ? $categories: array();
}

function archive_category_posts_query( string $tax, string $term ) {
  $args = array(
    \'post_type\'   => array(\'custom_post\', \'custom_post\', \'custom_post\'),
    \'numberposts\' => -1,
    \'orderby\'     => \'title\',
    \'order\'       => \'ASC\'
    \'tax_query\' => array(
      array(
          \'taxonomy\' => $tax,
          \'field\'    => \'slug\',
          \'terms\'    => $term,
      ),
    ),
  );
  return new WP_Query($args);
}

$categories = archive_categories(\'archive_category\');
$col_count = 4;

foreach ($categories as $category) :
  $category_posts_query = archive_category_posts_query($category->taxonomy, $category->slug);
  $post_count = $category_posts_query->found_posts;
  $posts_per_col = ceil( $post_count / $col_count );
  $i = 0;
  ?>
  <div>
    <a data-toggle="collapse" href="#<?php echo $category->slug; ?>">
      <h3><?php echo $count; ?><?php echo $category->name; ?></h3>
    </a>
    <div class="collapse" id="<?php echo $category->slug; ?>">
      <ul>
        <?php foreach ( $category_posts_query->posts as $category_post ) : 
          if ( $posts_per_col === $i ) {
            $i = 0; 
            echo \'</ul><ul>\';
          }
        ?>
          <li>
            <a href="<?php esc_url( get_the_permalink( $category_post->ID ) ); ?>"><?php echo esc_html( $category_post->post_title ); ?></a>
          </li>
        <?php $i++; endforeach; ?>
      </ul>
    </div> <!-- collapse -->
  </div> <!-- collapse wrapper -->
<?php endforeach;

EDIT 5.2.2020

分类到列?你是说这样的事?

$categories = archive_categories(\'archive_category\');
$category_count = count( $categories );
$col_count = 3;
$categories_per_col = $category_count > $col_count ? ceil( $category_count / $col_count ) : 1;
$column_index = 1;

echo \'<div class="row">\';
foreach ($categories as $category) :
  $category_posts_query = archive_category_posts_query($category->taxonomy, $category->slug);
  if ( $column_index > $col_count ) {
    $column_index = 1;
    echo \'</div><div class="row">\';
  }
  $classes = array(
    \'column_1_of_\' . $col_count,
    \'column-\' . $column_index,
  );
  ?>
  <div class="<?php echo implode( \' \', $classes ); ?>">
    <a data-toggle="collapse" href="#<?php echo $category->slug; ?>">
      <h3><?php echo $category->count . \' \' . $category->name; ?></h3>
    </a>
    <div class="collapse" id="<?php echo $category->slug; ?>">
      <ul>
        <?php foreach ( $category_posts_query->posts as $category_post ) : ?>
          <li>
            <a href="<?php esc_url( get_the_permalink( $category_post->ID ) ); ?>"><?php echo esc_html( $category_post->post_title ); ?></a>
          </li>
        <?php endforeach; ?>
      </ul>
    </div> <!-- collapse -->
  </div> <!-- collapse wrapper -->
<?php $column_index++; endforeach;
echo \'</div>\';

SO网友:Waldo Rabie

答案是CSS,你有几个选择!

<?php $categories = get_terms(\'my_category\'); ?>

<ul>
    <?php foreach ( $categories as $index => $category ): ?>
        <li>
            <a href="<?php the_permalink(); ?>"><?php echo get_the_title( $p->ID ); ?></a>
        </li>
    <?php endforeach; ?>
</ul>
您可以使用Flexbox。。。

ul {
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
  height: 306px;
  width: 200px;
}
li {
  width: 100px;
  height: 100px;
}
或者,可以使用文本列

ul {
  column-count: 2;
  column-gap: 0;
}
li {
  display: inline-block;
}
Here\'s 有更多细节和代码笔的优秀资源。

相关推荐

WordPress Recent Posts - Loop

我非常感谢您的帮助,我在列表项中写了一个循环,但我希望在缩略图上设置宽度和高度,并显示一些专家级的“文本”,但限制其长度。。。<ul> <!-- Define our WP Query Parameters --> <?php $the_query = new WP_Query( \'posts_per_page=3\' ); ?> <!-- Start our WP Query -->