Support » Fixing WordPress » get & display custom field data from child pages in table

  • I am trying to get and display the custom field data from the child pages of a particular page ($post_parent => 301;). The idea is to build a table where each row represents a child page, sorted by page_title of each child page.

    I need this to display ON the parent page AFTER the_content(); of the parent page in a table structure. I’d like to manually write the first row with <th> elements as the table headings.

    I’ve tried many of the examples I’ve read in the forum posts using query_posts() and get_posts() but I cannot figure it out.

    Should I first run the main loop displaying the parent page’s title and content, THEN run a new query to get the data from the child pages of the parent page?

    If yes, how would I do that? Then, how do I output the table, first row of headers (as headings for the data of each child) and then create a new row for each child’s data contained in its custom fields?

    I know how to use c2c’s get_custom() function to return this data for a page (at least when ON that page)… so I was hoping to write each row using something like this for each child page:

    <?php echo c2c_get_custom('Org-Name','<td><strong>','</strong></td>','','');
    echo c2c_get_custom('Services-Categories','<td>','</td>',' ','</td><td>','</td>'); ?>

    There are so many examples but they seem mostly used to generate a list of the child pages rather than access each child’s custom field data.

    Ideas? Examples?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Here’s an example to play with – replace the get_post_custom with your c2c…code.

    //in page, get children of this page and display with custom field 'cf1'
    $parent_id = $posts[0]->ID;
      'post_parent' => $parent_id,
      'post_type' => 'page',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      echo 'List of Posts';
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        $meta = get_post_meta($post->ID, 'cf1', true);
        if ($meta){
          echo 'customfield1: '. $meta;
    wp_reset_query();  // Restore global post data stomped by the_post().

    Thanks, Michael, I found another post after I first posted that also included your above code… and have been trying to modify it to work… but no joy… it worked, but there was no data… hmmm, somethings wonky.

    THEN I realized that what I want is the child pages of another page… for example, the page I am displaying this all on is ‘Program Partners’ which has the id of ‘301’. It has some child pages:
    Partner Organizations (A-Z) (323)
    Partners by Service Category (324)

    I want to get the custom field data from Partner Organizations, 9which is 323) to display the meta keys of ‘Org-Name’ and ‘Service-Categories’ using the c2c function so I can put html tags before, after, between, after last, etc for each child page.

    I’m thinking I have to run the main loop and display that page’s content, etc… following it by an <?php endwhile; ?>

    Then… run a new query for p=323 and use something like <?php get_pages(‘arguments’); ?> to display/echo those custom field values.

    Then, I’d follow it with endwhile; and before the get_footer(), use an endif;

    But I keep getting a parsing error – Parse error: syntax error, unexpected T_ENDWHILE in[blahblah]

    Here’s what I am using for both loops, starting from my main content div (mc) and ending the page where I get my footer:

    <div id="mc">
    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    <div class="post mpi" id="post-<?php the_ID(); ?>">
    <h2 class="toptitle"><?php the_title(); ?></h2>
    <?php the_content(); ?>
    <p class="postmetadata"><?php edit_post_link('Edit this content ↑', '', ''); ?></p>
    <?php endwhile; ?>
    <table class="spreadsheet"><tr>
    <th>Organization Name</th>
    <th>Public Safety</th>
    <th>Youth Development</th>
    <th>Family Support</th>
    <th>Financial Independence & Community Economic Development</th>
    <?php $my_query = new WP_Query('p=323');
    $args = array(
    'child_of' => 323,
    'parent' => 323,
    'sort_order' => 'ASC',
    'sort_column' => 'post_title',
    'hierarchical' => 0
    ); ?>
      $pages = get_pages('child_of=323&parent=323&sort_column=post_title&sort_order=ASC&hierarchical=0');
      foreach ($pages as $page) {
    echo c2c_get_custom('Org-Name','<tr><th>','</th>','','');
    echo c2c_get_custom('Service-Categories','<tr><td>','</td>',' ','</td><td>','</td></tr>'); }
    <?php endwhile; ?>
    <?php include (TEMPLATEPATH . '/tabbercontent.php'); ?>
    <?php endif; ?>
    <?php get_footer(); ?>

    Totally off base? Suggestions?

    THEN I realized that what I want is the child pages of another page

    You are likely going to need to use c2c_get_post_custom to get your custom field where $page->ID should be the $post_id value:

    <?php function c2c_get_post_custom( $post_id, $field, $before='', $after='', $none='', $between='', $before_last='' ) ?>

    Thank you SO much! I hadn’t thought to look within Scott’s plugins for a solution.

    I’ll post back when and if I get it to work.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘get & display custom field data from child pages in table’ is closed to new replies.