I would like to show like below in HTML with PHP.
It assumes there are six categories.
<div class='box'><p>category 1</p><p>category2</p></div>
<div class='box'><p>category 3</p><p>category 4</p></div>
<div class='box'><p>category 5</p><p>category 6</p></div>
However, I don’t know how I do it with PHP.
I did below, but it shows only category 1&2.
<?php $categories = get_categories();foreach ($categories as $category):?>
<div class="sheif"><!-- genre 1 -->
<div class="sheif__inner">
</div>
<div class="sheif__body">
<?php $categories = get_categories(); $x=1; $num=3; foreach ($categories as $category): ?>
<?php if( $x>=$num ) { break; } else { ?>
<div class="sheif__book">
<a href="<?php echo esc_url(get_category_link($category->term_id)); ?>" class="book-btn">
<img src="<?php echo z_taxonomy_image_url($category->term_id); ?>" alt="本">
</a>
<a href="<?php echo esc_url(get_category_link($category->term_id)); ?>" class="bar-genre">
<p><?php echo $category->name; ?></p>
</a>
</div>
<?php } $x++ ; ?>
<?php endforeach; ?>
</div>
<div class="sheif__side-bar">
<p>ジャンルメニュー</p>
</div>
</div><!-- genre 1 -->
<?php endforeach; ?>
Please teach me how it works I want.
3
Answers
This will output the two value side by side. Array_chunk is a php function that splits an array into chunks, once you get that result you can output it any way, here for example using lists but you can change it to paragraphs.
https://www.php.net/manual/en/function.array-chunk.php
You can use array_chunk to split an array into different groups & run your loop on this group to make it work.
Try this
wp_get_post_categories( int $post_id, array $args = array() )
wp_get_post_categoriesOR use
get_terms( array|string $args = array(), array|string $deprecated = '' )
get_terms