How to list Pages with custom fields in 2 columns in WordPress
Recently, I needed to list all subpages of a parent page. I also needed to display custom fields below each subpage. And, on top of that, the list needed to be 2 columns.
I found a solution for simply listing the pages in 2 columns, but it did not suggest how to modify the code to include custom fields.
So, thanks to our coding hero Mark Kaplun who supplied the below code, although he admits it’s a “dirty hack”. The sort_column is so that MyPageOrder would work properly for this listing.
<?php
$args = array(
‘post_type’ => ‘page’,
‘numberposts’ => 40,
‘sort_column’ => ‘menu_order’,
‘child_of’ => 84 // any parent
);
$attachments = get_pages($args);
//print_r(“attachements:” . $attachements);
echo ‘<div style=”float: left; margin: 0 10px 0 0;”>’;
?><?php
for ($i =0; $i<count($attachments)/2; $i++) {
$post = $attachments[$i];
// foreach ($attachments as $post) { ?><a href=”<?php echo get_permalink($post->ID) ?>” rel=”bookmark”><?php echo $post->post_title; ?></a>
<?php if(get_post_meta($post->ID, “NameofCustomField”, true)) { ?>
<?php
echo get_post_meta($post->ID, “NameofCustomField”, true);
}
?>
<?php } ?>
</div>
<?php
echo ‘<div style=”float: left; margin: 0 10px 0 0;”>’;
for (; $i<count($attachments); $i++) {
$post = $attachments[$i];
// foreach ($attachments as $post) { ?><a href=”<?php echo get_permalink($post->ID) ?>” rel=”bookmark”><?php echo $post->post_title; ?></a>
<?php if(get_post_meta($post->ID, “PracticeArea”, true)) { ?>
<?php
echo get_post_meta($post->ID, “PracticeArea”, true);
}
?>
<?php } ?>
</div>
Category: Code Snippets








I love WP garage for this kind on technical wp hacks/tips. I follow a quite large quantity of design/wp sites but am glad to see an upper level here. Keep it up!
I am looking to create a list of subpages. Simply adding the necessary code to the page template in order to create a subpage listing table. With that being said I want the table to be 5 rows wide and also grab a thumbnail custom field. Any tips?
Thank you very much for this! I’ve been searching for hours for something like this!
Mike
Hi, seems perfect but cant get anything to display just a blank empty page, any suggestions by the way im using WP 3.0.1
You have to replace the curly apostrophes and quotes with straight ones for it to work.