WordPress.org

Ready to get started?Download WordPress

Forums

How to display sub pages in columns (5 posts)

  1. rikardo85
    Member
    Posted 1 year ago #

    Hello,

    I would like to display sub pages in columns.
    Some parent pages have a lot of sub pages and the scroll is very long.

    I am currently using the below code to display sub pages.

    <?php
      if($post->post_parent)
      $children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
      else
      $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
      if ($children) { ?>
      <div id="subNav">
    
    <ul>
      <?php echo $children; ?>
      </ul>
      </div>
      <?php } ?>

    Any help much appreciated.

    Thanks

  2. some related code I used a while ago:

    <?php //to auto break list of pages into colunms
    $list = wp_list_pages('title_li=&depth=1&echo=0'); //keep 'echo=0' and 'depth=1'; add all your parameters;
    $list_elements = explode('</li>', $list);
    $num = count($list_elements)-1;
    
    $col = 3; //set the number of columns
    $num_col = ceil($num/$col);
    $width = floor(100/$col)-5;
    $style = 'width:'.$width.'%;margin-right:2%;padding-left:3%;background: #f7f7f7;float:left;';
    
    for( $i=1; $i<$col; $i++) { $list_elements[$i*$num_col] = '</ul><ul style="'.$style.'">'.$list_elements[$i*$num_col]; }
    $list = implode('</li>', $list_elements); ?>
    
    <ul style=" <?php echo $style; ?>">
    <?php echo $list; ?>
    </ul>
    <div style="clear:both;width:100%;display:block;">&nbsp;</div>

    if you have problems adjusting this to your code, please post here again.

  3. rikardo85
    Member
    Posted 1 year ago #

    Thank you for your feedback.

    It's very nearly there.

    I added the following code for the sub pages to only be displayed under the parent page.

    $list = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0"); //keep 'echo=0' and 'depth=1'; add all your parameters;

    The 'ul' tags are appearing on every page. Can you guide me on how to put these in a conditional statement so that they only appear when there is child pages?

    Also, Can you please guide me on how I increase the list length for each column? It is currently displaying 2 pages per column.

    Thanks

  4. The 'ul' tags are appearing on every page. Can you guide me on how to put these in a conditional statement so that they only appear when there is child pages?

    after the second line of my suggested code, add:

    if( $list ) {

    and after the last line add:

    <?php } ?>

    how I increase the list length for each column? It is currently displaying 2 pages per column.

    haw many pages do you have in the list?
    the code is desinged to split, whatever number of pages are in the list, into the number of columns set in this line $col = 3; //set the number of columns
    to have longer columns, you might need to reduce that number to 2 (?)

  5. rikardo85
    Member
    Posted 1 year ago #

    Thank you so much.

    I have one last question.
    When I am on a child page, the sub child pages do not appear.

    Do you know how to display the child pages when you are on another child page from the same parent?

    Thanks

Topic Closed

This topic has been closed to new replies.

About this Topic