treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Pulling custom field data from child pages

  • Hey guys,

    Wondering if this is possible, maybe ye can help me:

    I have a list at the bottom of each individual page listing all the other pages of that category (i.e. pages that have the same 'parent' page), for which I am using the following code. You can see a live example here: http://oldkitbag.com/cashelbay-wp/past-ponies/jjs-fiona/


    <?php
    if($post->post_parent)
    $children = wp_list_pages(\"title_li=&child_of=\".$post->post_parent.\"&echo=0\");

    else

    $children = wp_list_pages(\"title_li=&child_of=\".$post->ID.\"&echo=0\");
    if ($children) { ?>

    <ul id=\"see-all\">
    <li id=\"see-all-title\"><h3>
    <?php
    if (!empty($post->post_parent)) {
    $parentTitle = get_the_title($post->post_parent);?>
    See all Ponies listed under <?php echo $parentTitle; ?>:
    <?php } ?>
    </h3></li>
    <?php echo $children; ?>
    </ul>
    <?php } ?>


    It works no probs, it just looks crap. Now each of these child pages have custom fields, including thumbnails, and I'm wondering would it be possible to construct a list that not only coughs up the page titles but also the thumbnail custom field for each page as well. Any help would be greatly appreciated - even if it's just to say its not possible! :D
  • You gotta run a loop if you want custom fields =)
  • I came accross the following at http://www.wprecipes.com/wordpress-how-to-get-custom-fields-outside-the-loop. Now, I think they are still running a loop per se - isn't that what a wp_query is? But anyway, my point is this: could I use this code in some form to echo just the 'thumbnail' custom fields and the titles, both suitably permalinked, of all the pages of a given category, as in my original problem?

    To display a custom field value outside the loop, simply use the following code. Don't forget to replace customField on line 4 by the name of the custom field you want to display.

    <?php
    global $wp_query;
    $postid = $wp_query->post->ID;
    echo get_post_meta($postid, 'customField', true);
    ?>