skip to Main Content

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


  1. You can follow the example below. 
    
    $categories = array("category1", "category2", "category3", "category4");
    foreach (array_chunk($categories, 2) as $chunk) {
        echo "<li>" . implode(', ', $chunk) . "</li>n";
    }
    

    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

    Login or Signup to reply.
  2. You can use array_chunk to split an array into different groups & run your loop on this group to make it work.

    foreach (array_chunk(get_categories();, 2) as $group) {
        //render item
        foreach ($group as $item) {
           <div class='box'><p>$item[0]->name</p><p>$item[1]->name</p></div>
        }
    
    }
    
    Login or Signup to reply.
  3. Try this wp_get_post_categories( int $post_id, array $args = array() ) wp_get_post_categories

    $post_categories = wp_get_post_categories( $post_id, array( 'fields' => 'all', 'number'=>'2' ) );
    if( $post_categories ){ // Always Check before loop!
        foreach( $post_categories as $c ){
            //your code here
        }   
    }
    

    OR use get_terms( array|string $args = array(), array|string $deprecated = '' ) get_terms

    $terms = get_terms( array(
        'taxonomy' => 'taxonomy_name',
        'hide_empty' => false,
        'number' => 2
    ) );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search