WordPress.org

Forums

Loop Order? (10 posts)

  1. acondiff
    Member
    Posted 5 years ago #

    I've got a loop here and I was wondering if I can modify this so that the child pages show up in a specific order. Right now I think they appear in alphabetical order. It doesnt seem to listen to the order numbers that I specify in the wordpress admin. Any help would be great. Here is my code:

    <?php $pages = get_pages(array('child_of' => 5)); ?>
        	<?php foreach ($pages as $page): ?>
            <div class="threecol">
            <a href="<?php echo get_permalink($page->ID); ?>">
            <?php echo get_the_post_thumbnail($page->ID, array(250,200)); ?></a>
            <h4><a href="<?php echo get_permalink($page->ID); ?>">
            <?=$page->post_title?></a></h4>
            <p><?=$page->post_content?></p>
            </div>
        	<?php endforeach; ?>

    Thanks!
    Austin

  2. esmi
    Forum Moderator
    Posted 5 years ago #

    Try

    $args = array(
    	'child_of' => 5,
    	'sort_column' => 'menu_order'
    	);
    $pages = get_pages($args);

    http://codex.wordpress.org/Function_Reference/get_pages

  3. acondiff
    Member
    Posted 5 years ago #

    thanks alot! that did the trick.

    one last question. in - $pages = get_pages(array('child_of' => 5)

    instead of child of the page with an id of 5, how do I get it to detect the current pages id and stick it in the place of 5, so that it is loading child pages of the page you are currently on? (if that makes any sense at all)

    thanks again!

  4. esmi
    Forum Moderator
    Posted 5 years ago #

    Try $pages = get_pages(array('child_of' => $post->ID)

  5. acondiff
    Member
    Posted 5 years ago #

    ok back to my first question. so if i have any children of children it displays them as well (which i dont want them to be displayed). so i tried:

    'max_depth' => 1,

    it doesnt work though. any suggestions?

  6. esmi
    Forum Moderator
    Posted 5 years ago #

    Try

    <?php $args = array(
    	'child_of' => $post->ID,
    	'parent' => $post->ID
    	);
    $pages = get_pages( $args);?>
  7. acondiff
    Member
    Posted 5 years ago #

    thats a lot. ok last question i promise. how do you get it to list the children of the parent?

  8. esmi
    Forum Moderator
    Posted 5 years ago #

    There's an example of just that on the link I gave above.

  9. acondiff
    Member
    Posted 5 years ago #

    im sorry but i didn't find the solution on that page.

    here is what i wanted to accomplish:

    in all of the main pages (pages that appear in nav bar) show the children of those pages. then when you get into children of those pages display the children of that top parent page. so however deep in the site you are it still displays the children of that top page.

  10. esmi
    Forum Moderator
    Posted 5 years ago #

    That's a lot more complex. You'll need to test the current page to see if it has a parent. Then test the parent the same way and so on - back up the tree - until you reach a page that doesn't have a parent. Then you can be sure you've reached the top level page whose id you can use to display the child pages as above.

    There are a couple of examples in http://codex.wordpress.org/Conditional_Tags#A_PAGE_Page Snippets 2 and 4 would seem to provide the best foundation for creating a function that you could use to walk back up the page tree.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags