几年前我也有类似的问题。这是我发表的一个要点,它展示了我如何解决这个问题的基本想法,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>\';