WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Categories in two columns (3 posts)

  1. martinj86
    Member
    Posted 3 years ago #

    Hello everyone,

    I was wondering whether anyone could help with what I think is a relatively straightforward problem (having said that, I can't for the life of me figure out how to get it to work!)

    I'm trying to display my list of categories in two columns - but I need to display some additional content before each category is outputted (basically, it's a list of countries, and I'm trying to output a country flag before each category name).

    I have the code for both parts and both work perfectly when used independently ... but I just can't figure out how to merge the two together.

    Here's the code for the two columns:

    <?php
    $cats = explode("<br />",wp_list_categories('title_li=&echo=0&depth=1&style=none&child_of=7'));
    $catsname =
    $cat_n = count($cats) - 1;
    for ($i=0;$i<$cat_n;$i++):
    if ($i<$cat_n/2):
    $cat_left = $cat_left.$cats[$i].'<br />';
    elseif ($i>=$cat_n/2):
    $cat_right = $cat_right.$cats[$i].'<br />';
    endif;
    endfor;
    ?>
    <div class="left">
    <?php echo $cat_left;?>
    </div>
    <div class="right">
    <?php echo $cat_right;?>
    </div>

    And here's the code I'm using to output the list of categories, with an image of the country flag for each:

    <?php
      $categories = get_categories('child_of=7');
      foreach ($categories as $cat) {
      echo '<img src="url/to/image/goes/here/'.$cat->cat_name.'.png" alt="'.$cat->cat_name.'" /> <a href="'.get_category_link( $cat->cat_ID ).'">'.$cat->cat_name.'</a><br />';
      }
    ?>

    If anyone can come up with a solution, I would be really, really grateful.

    Thanks all,

    Martin

  2. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    not quite a combination of both methods:

    <?php
      $categories = get_categories('child_of=7');
    $count = 1; ?>
    <div class="left">
    <?php
      foreach ($categories as $cat) {
      echo '<img src="http://www.totalbackpacker.co.uk/wp-content/themes/totalbackpacker/images/countries/'.$cat->cat_name.'.png" alt="About" />&nbsp;<a href="'.get_category_link( $cat->cat_ID ).'">'.$cat->cat_name.'</a><br />';
    if( $count == ceil( count($categories)/2 ) ) { ?>
    </div>
    <div class="right">
    <?php }
    $count++;
      }
    ?>
    </div>
  3. martinj86
    Member
    Posted 3 years ago #

    Thanks a million mate, that works a treat!
    Have a great night,
    Martin

Topic Closed

This topic has been closed to new replies.

About this Topic