WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Query Child Pages of a Current Page and Loop Through Each Child Page (9 posts)

  1. equaldesign
    Member
    Posted 4 years ago #

    First of all sorry for the long title. I am currently using the code below to display a list of child pages or a specific parent page (in this case parent page_id 7) and then show thumbnails from a custom field attached to each parent page.

    <?php query_posts(array('showposts' => 20, 'post_parent' => 7, 'post_type' => 'page')); while (have_posts()) { the_post(); ?>
    
            <div class="blind-subpage">
    
                <?php if ( get_post_meta($post->ID, "articleimg", true) ) { ?>
    
                    <a href="<?php the_permalink(); ?>"><img src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?src=<?php echo get_post_meta($post->ID, "articleimg", true); ?>&w=120&h=120&zc=1" alt="<?php the_title(); ?>" /></a>
    
                <?php } else { ?>
    
                    <a href="<?php the_permalink(); ?>"><img src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?src=<?php bloginfo('template_url'); ?>/images/blinds-subpage-image.jpg&w=120&h=120&zc=1" alt="<?php the_title(); ?>" /></a>
    
                <?php } ?>
    
            <!-- closes blind-subpage -->
            </div>
    
        <?php } ?>

    What can I put in place of the 'post_parent=7' so that it will do this for the current page you are on? It is probably really easy and sorry if I am being thick here!!

    Thanks in advance.

  2. MichaelH
    Member
    Posted 4 years ago #

    $this_page_id=get_query_var('page_id')

    so

    'post_parent' => $this_page_id,

    Might use this in your page template to see all the variables...

    echo "<pre>"; print_r($wp_query->query_vars); echo "</pre>";
  3. equaldesign
    Member
    Posted 4 years ago #

    Thanks for that info, however when I use this code it is telling me that the page_id is '0' when it is clearly 3. Any ideas?

  4. equaldesign
    Member
    Posted 4 years ago #

    Got it working with the following code:

    <?php $this_page_id=$wp_query->post->ID; ?>
    
        	<?php query_posts(array('showposts' => 20, 'post_parent' => $this_page_id, 'post_type' => 'page')); while (have_posts()) { the_post(); ?>
    
                <div class="subpage-img">
    
                    <?php if ( get_post_meta($post->ID, "post-img", true) ) { ?>
    
                        <a href="<?php the_permalink(); ?>"><img class="subpage-customimg" src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?src=<?php echo get_post_meta($post->ID, "post-img", true); ?>&w=115&h=90&zc=1" alt="<?php the_title(); ?>" /></a>
    
                        <h3><?php the_title(); ?></h3>
    
                    <?php } else { ?>
    
                        <p>This is a sub page of this one - <?php the_title(); ?></p>
    
                    <?php } ?>
    
                <!-- closes subpage-img -->
                </div>
    
            <?php } ?>

    So that gets all child pages of the current page, then loops through each child page , in this case showing a custom field in an image tag, using timthumb in order to auto resize the image.

    Thanks for your help MichaelH.

  5. Darren_S
    Member
    Posted 4 years ago #

    Thanks, equaldesign! This is a huge help. I am off to see how many different custom fields I can get away with displaying on the parent page, to create a fancy section menu in the main body.

  6. Darren_S
    Member
    Posted 4 years ago #

    Trouble in paradise. Anyone have any ideas why the code above would display correctly when the template is used on one page, but fail to display the child items when the same template is used for a different page elsewhere in the tree?

    Second: On the page where it does work, the items are being displayed in descending order. How can I display the items in page_order?

  7. Darren_S
    Member
    Posted 4 years ago #

    Fixed the first item, but the second is still thwarting me. My child pages are displaying in reverse chronological order, and I can't seem to affect the sorting at all using the order and orderby attributes of query_post.

    Here's the relevant code:

    <?php /* THE LOOP */
    if (have_posts()) : ?>
    
    	<?php /* This outputs the next/previous post or page navigation.
    	This can be edited at Atahualpa Theme Options -> Style & edit the Center column */
    	bfa_center_content($bfa_ata['content_above_loop']); ?>
    
        	<?php query_posts(array('showposts' => -1, 'post_parent' => $post->ID, 'post_type' => 'page')); while (have_posts()) { the_post(); ?>

    I am using WP 2.8.6 with the index.php template in the Atahualpa theme.

    Again, I've tried the order and orderby attributes in various combinations and placements -- in this query_posts tag, in a separate one outside the loop, inside the loop, etc.

    I think one thing that is throwing me off is that Atahualpa splits up "if (have_posts())" from "while (have_posts()) { the_post()". Not sure if this is relevant at all, but everything I've found in the codex and the forum indicates that something like "order=ASC&orderby=menu_order" should work perfectly.

    Thanks to anyone for the help.

  8. timbednar
    Member
    Posted 4 years ago #

    I had this same issue.

    query_posts(array('orderby' => 'menu_order','showposts' => $showposts, 'post_parent' => $thePostID, 'post_type' => 'page'));

    When I used bother orderby AND order, everything worked out. Now I'm testing on WP 2.9.1.

    query_posts(array('orderby' => 'menu_order', 'order' => 'ASC','showposts' => $showposts, 'post_parent' => $thePostID, 'post_type' => 'page'));

    /Tim
    --
    http://ashford.turtleinteractive.com/

  9. greyhat2010
    Member
    Posted 3 years ago #

    Thanks a lot for this, really helped.

    :D

Topic Closed

This topic has been closed to new replies.

About this Topic