logo

$30
Trying to get subpages with a loop. What's wrong?

Hi there,

This is a great site by the way! Just what we've needed in the WP community.

I am stumped with this problem. I am trying to get sub-pages and then loop through them, listing all of the sub-pages and then outputting their individual thumbnail images, names and descriptions with custom fields.

Something is not working but I can't figure out what it is. I'm on a tight deadline so I'm a tad desperate. If anyone can figure this out I'd be grateful!

Thanks much.

<?php $id_of_the_parent_page = 218;  ?>
<?php $args = array(
'sort_column' => 'menu_order'
);?>
<?php $pages = get_pages($args); ?>
<?php $allChildrenPages = & get_page_children( $id_of_the_parent_page, $pages ) ?>
<?php for ($i=0; $i < count($allChildrenPages); $i+) { ?>
<?php $thisChildPage = $allChildrenPages[$i]; $childId = $thisChildPage->ID; ?>

<?php $key="choose-profession" ?>
<?php $value= get_post_meta($thisChildPage->ID, $key, true); ?>

<?php
if ($value=="actors"): ?>
<div class="item">
<div class="thumb">
<div class="border">
<a href="<?php echo get_permalink($thisChildPage->ID); ?>"><img src="<?php echo get_post_meta($childId, "thumb_img _url", true) ?>" alt="<?php echo $thisChildPage->post_title; ?>"/></a>
</div>
</div>
<div class="item-info">
<h4><a href="<?php echo get_permalink($thisChildPage->ID); ?>"><?php echo $thisChildPage->post_title; ?></a></h4>
<p><?php echo get_post_meta($childId, "description", true) ?></p>
</div>
<div class="clear"></div>
</div>
<?php endif ?>

<?php } ?>

Ron Jenkins | 12/08/09 at 10:03pm | Edit


(6) Possible Answers Submitted...

  • avatar
    Last edited:
    12/08/09
    10:45pm
    steve says:

    Would this plug-in have your desired effect?

    http://www.dagondesign.com/articles/list-subpages-plugin-for-wordpress/

  • avatar
    Last edited:
    12/08/09
    11:01pm
    Justin Tadlock says:

    This will list all subpages of a page ID of 218 with the required custom fields being shown. I also added in a few checks to make sure there's information before displaying anything.

    <?php

    $parent_id = 218;
    $pages = get_pages( array( 'sort_column' => 'menu_order', 'child_of' => $parent_id ) );

    if ( is_array( $pages ) ) {

    foreach ( $pages as $page ) {

    $profession = get_post_meta( $page->ID, 'choose-profession', true );

    if ( 'actors' == $profession ) { ?>

    <div class="item"><?php

    $thumbnail = get_post_meta( $page->ID, 'thumb_img _url', true );
    $description = get_post_meta( $page->ID, 'description', true );

    if ( $thumbnail ) { ?>

    <div class="thumb">
    <div class="border">
    <a href="<?php echo get_permalink( $page->ID ); ?>"><img src="<?php echo $thumbnail; ?>" alt="<?php esc_attr( $page->post_title ); ?>" /></a>
    </div>
    </div>

    <?php } if ( $description ) { ?>

    <div class="item-info">
    <h4><a href="<?php echo get_permalink( $page->ID ); ?>"><?php echo $page->post_title; ?></a></h4>
    </div>

    <?php } ?>

    <div class="clear"></div>

    </div>

    <?php }
    }
    } ?>

  • avatar
    Last edited:
    12/08/09
    11:03pm
    Justin Tadlock says:

    Ahh! Where's the edit button? I posted the incorrect version. Please use this version instead:

    <?php

    $parent_id = 218;
    $pages = get_children( array( 'orderby' => 'menu_order', 'post_parent' => $parent_id ) );

    if ( is_array( $pages ) ) {

    foreach ( $pages as $page ) {

    $profession = get_post_meta( $page->ID, 'choose-profession', true );

    if ( 'actors' == $profession ) { ?>

    <div class="item">

    <?php $thumbnail = get_post_meta( $page->ID, 'thumb_img_url', true ); ?>

    <?php if ( $thumbnail ) { ?>

    <div class="thumb">
    <div class="border">
    <a href="<?php echo get_permalink( $page->ID ); ?>"><img src="<?php echo $thumbnail; ?>" alt="<?php esc_attr( $page->post_title ); ?>" /></a>
    </div>
    </div>

    <?php } // End check for thumbnail ?>

    <?php $description = get_post_meta( $page->ID, 'description', true ); ?>

    <?php if ( $description ) { ?>

    <div class="item-info">
    <h4><a href="<?php echo get_permalink( $page->ID ); ?>"><?php echo $page->post_title; ?></a></h4>
    <p><?php echo $description; ?></p>
    </div>

    <?php } // End check for description ?>

    <div class="clear"></div>

    </div>

    <?php } // End check for profession

    } // End foreach loop through pages

    } // End check for pages ?>

  • avatar
    Last edited:
    12/11/09
    8:49pm
    Tim Holt says:

    Is the problem that the i+ in your for loop should be i++?

  • avatar
    Last edited:
    12/09/09
    11:30pm
    Darran Low says:

    Instead of using the page ID, I am using the function get_page_by_title() to retrieve the ID. It would allow you to use this code on multiple installations. Just change Your Page Name to the title of your parent page. Hope this helps.

    <?php
    $parent_page_id = get_page_by_title('Your Page Name');
    $parent_children = get_pages('child_of=' . $post->ID);

    foreach($parent_children as $child) {
    $profession = get_post_meta($child->ID, 'choose-profession', true);
    if ($profession == 'actors') {
    ?>
    <div class="item">
    <div class="thumb">
    <div class="border">
    <a href="<?php echo get_permalink($child->ID); ?>" title="<?php echo $child->post_title ?>">
    <img src="<?php echo get_post_meta($child->ID, "thumb_img_url", true) ?>" alt="<?php echo $child->post_title; ?>"/>
    </a>
    </div>
    </div>

    <div class="item-info">
    <h4><a href="<?php echo get_permalink($child->ID); ?>"><?php echo $child->post_title; ?></a></h4>
    <p><?php echo get_post_meta($child->ID, "description", true) ?></p>
    </div>

    <div class="clear"></div>
    </div>

    <?php
    }
    }
    ?>

  • avatar
    Last edited:
    12/10/09
    7:12pm
    Darran Low says:

    Oops. I can't seem to edit my post.

    Change the following

    $parent_page_id = get_page_by_title('Your Page Name');


    to


    $parent_page = get_page_by_title('Your Page Name');
    $parent_page_id = $parent_page->ID;

This question has expired.





Current status of this question: Completed