Support

Account

Home Forums General Issues Split categories list w/custom field into two columns

Helping

Split categories list w/custom field into two columns

  • Hi!
    I’m trying to get a list of categories that has a single custom field, into two columns. So far so good, i can get the list in a single column with this code:

                  <?php
                  $args = array( 'hide_empty' => '0');
                  $categories = get_categories($args);
                  if($categories){
                    echo '<div class="col"><ul class="cat-list">';
                    foreach($categories as $category) {
                      $color = get_field('color', 'category_'.$category->term_id);
                      echo '<li style="background-color:'.$color.'">';
                      echo '<a href="';
                      echo esc_url( get_category_link( $category->term_id ) );
                      echo '">';
                      echo $category->name;
                      echo '</a>';
                      echo '</li>';
                    } 
                    echo '</ul></div>';
                  }    
                  ?>

    Now i’m trying to get the list using wp_list_categories (tip: https://wpsquare.com/display-wordpress-categories-two-columns/). This works as a solution for the two columns issue, but with this method i can’t retrieve the value from the category custom field.

    Thanks in advance.

  • Instead of using wp_list_categories, perhaps you could add some code to your first attempt and divide your $categories array into two arrays, then output each array in its own column:

    From Stackoverflow:
    How to split/divide an array into 2 using php

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Split categories list w/custom field into two columns’ is closed to new replies.