Horje
Trying to get subpages with a loop. What's wrong? - Wordpress Solution
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 rel="nofollow" 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 rel="nofollow" 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 } ?>


Solution - 1

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


Solution - 2

Would this plug-in have your desired effect? [[LINK href="http://www.dagondesign.com/articles/list-subpages-plugin-for-wordpress/"]]http://www.dagondesign.com/articles/list-subpages-plugin-for-wordpress/[[/LINK]]


Solution - 3

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 rel="nofollow" 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 rel="nofollow" href="<?php echo get_permalink( $page->ID ); ?>"><?php echo $page->post_title; ?></a></h4>
					</div>

				<?php } ?>

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

			</div>

		<?php }
	}
} ?>


Solution - 4

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 rel="nofollow" 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 rel="nofollow" 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 ?>


Solution - 5

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 rel="nofollow" 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 rel="nofollow" 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
		}
	}
?>


Solution - 6

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;





Wordpress

Related
I need to create a custom dropdown field but i need the values to be d - Wordpress Solution I need to create a custom dropdown field but i need the values to be d - Wordpress Solution
On submit upload files to server - Wordpress Solution On submit upload files to server - Wordpress Solution
wordpress ajax load more - Wordpress Solution wordpress ajax load more - Wordpress Solution
[ TABLECONTENT NOT RESPONSIVE ON SMALL SCREENS ] - Wordpress Solution [ TABLECONTENT NOT RESPONSIVE ON SMALL SCREENS ] - Wordpress Solution
Populate entries from repeater field in another field (same form) to add more details. - Wordpress Solution Populate entries from repeater field in another field (same form) to add more details. - Wordpress Solution

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
71