WordPress.org

Ready to get started?Download WordPress

Forums

Change List of subpages to be displayed in two columns – but keep alphabetical o (1 post)

  1. nichart
    Member
    Posted 3 years ago #

    I use the code below to list my subpages on specific page. However I would like to display the list in two columns.

    <?php
        global $wp_query;
        if( empty($wp_query->post->post_parent) )
          {
             $parent = $wp_query->post->ID;
          }
        else
         {
             $parent = $wp_query->post->post_parent;
         }
    ?>
    <?php if(wp_list_pages("title_li=&child_of=$parent&echo=0" )): ?>
        <div id="submenu1">
         <ul>
          <?php wp_list_pages("title_li=&child_of=$parent" ); ?>
         </ul>
        </div>
    <?php endif; ?>

    I have tried to use CSS to float it into two columns, but I get the following outcome:

    Page title A Page title B
    Page title C Page title D
    Page title E Page title F

    So I'm hoping someone can help tweak the code so is display in two columns and with the follow order:

    Page title A Page title D
    Page title B Page title E
    Page title C Page title F

    In a 'PHP forum' a kind soul told me to:

    Count the total number of results, and then split it in two (if there are an odd number of results make the first column have the extra one). Then loop through the results until you get half way; end your first column, and start the second. Here is an example:

    <?php
       $total_results = count($results);
       $col_1_total = ceil($total_results/2);
       $col_2_total = $total_results - $col_1_total;
    ?>
    <ul id="col_1">
    <?php
       $i=0;
       foreach($results as $result){
    
          // If the current number of iterated results is equal to the total number of results
          // for the first column, then end the first column and start the second.
          if($i == $col_1_total){
             echo '</ul><ul id="col_2">';
          }
    ?>
       <li><?php echo $result; ?></li>
    <?php
          $i++;
       }
    ?>
    </ul>

    But seeing as I'm a PHP newbie - I have no clue how to combine these codes?

    Any help would be much appreciated!

Topic Closed

This topic has been closed to new replies.

About this Topic