solostream

How to list Pages with custom fields in 2 columns in WordPress

| March 22, 2010 | 5 Comments

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>

0saves
If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Tags: , , ,

Category: Code Snippets

About Rebecca Markowitz: Rebecca Markowitz has built over 40 WordPress sites for clients as Web Marketing Manager and WordPress specialist at illuminea. illuminea is a Jerusalem-based boutique web agency. WPGarage shows my dedicated relationship with WordPress over the years - full of love, laughs, tears, growth and strong drinks. L'chaim! View author profile.

Elegant WP Themes

Comments (5)

Trackback URL | Comments RSS Feed

  1. kevin says:

    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!

  2. SEM Truth says:

    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?

  3. Mike says:

    Thank you very much for this! I’ve been searching for hours for something like this!
     
    Mike

  4. Fraser says:

    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

Leave a Reply




If you want a picture to show with your comment, go get a Gravatar.

More in Code Snippets (7 of 28 articles)