WordPress.org

Ready to get started?Download WordPress

Forums

Display certain custom field from each child page (2 posts)

  1. noworyz
    Member
    Posted 4 years ago #

    I am trying to create a page template that will display a custom field from each of the current page's child pages.

    MAIN PAGE
    -CHILD PAGE 1
    -CHILD PAGE 2

    on the Main page = show the 'customfield1' from each of the child pages.

    I want to write this up so it is not specific to a certain page and the template can be used by other main pages to show the custom field values from its chile pages.

    Thanks

  2. MichaelH
    Member
    Posted 4 years ago #

    In a Page Template:

    <?php
    $parent_id = $posts[0]->ID;
    $args=array(
      '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>
        <?php
        $meta = get_post_meta($post->ID, 'cf1', true);
        if ($meta){
          echo 'customfield1: '. $meta;
        }
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic