1

I have a parent page with some child pages inside. What I'm looking to do is display every child page excerpt and some other information inside its own cell on the parent page. Does anyone know how this can be achieved ?

<div class="parent page">

    <?php  $args = array(
          'parent' => $post->ID,
          'post_type' => 'page',
          'post_status' => 'publish'
      ); 
      $pages = get_pages($args);  ?>
      <div class="childcells"> 
      <?php foreach( $pages as $page ) { ?>
       <div class="childcell">
          <div class="thumbnail"><?php echo get_the_post_thumbnail($page->ID, 'small-thumb'); ?></div>
          <div class="myclasstitle"><?php echo $page->post_title; ?></div>
          <span class="desc"><?php echo get_post_meta($page->ID, 'desc', true); ?></span>
      <a href="<?php echo  get_permalink($page->ID); ?>" rel="bookmark" title="<?php echo $page->post_title; ?>">
            Read more
          </a>

      </div>

?>

5
  • what is the result of your code? what is different from what you expect?
    – Michael
    Apr 21, 2018 at 2:28
  • result is every other info except child page excerpt. i added ` <?php echo get_the_excerpt ($page); ?>` but i get an error
    – iman
    Apr 21, 2018 at 2:59
  • try get_the_excerpt( $page->ID ) codex.wordpress.org/Function_Reference/get_the_excerpt
    – Michael
    Apr 21, 2018 at 3:05
  • I tried that too, but same error appears
    – iman
    Apr 21, 2018 at 3:17
  • what is the error message?
    – Michael
    Apr 21, 2018 at 4:15

1 Answer 1

0

get_the_excerpt() won't work in your code if you add content to the main WYSIWYG editor instead of the excerpt field.

Check how get_the_excerpt() work and try to find out filter hook with get_the_excerpt hook name.

Please try the following code, hope you'll get the expected result.

<div class="parent page">
    <?php
    global $post;
    $args = array(
        'parent'      => $post->ID,
        'post_type'   => 'page',
        'post_status' => 'publish'
    ); 
    $children = get_pages( $args );

    if ( ! empty( $children ) ) :
        ?>
        <div class="childcells"> 
            <?php
            foreach ( $children as $post ) : setup_postdata( $post );
                ?>
                <div class="childcell">
                    <?php if ( has_post_thumbnail() ) : ?>
                        <div class="thumbnail"><?php the_post_thumbnail( 'small-thumb' ); ?></div>
                    <?php endif; ?>
                    <div class="myclasstitle"><?php the_title(); ?></div>
                    <span class="desc"><?php echo get_post_meta( get_the_ID(), 'desc', true ); ?></span>
                    <div class="excerpt"><?php the_excerpt(); ?></div>
                    <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">Read more</a>
                </div>
                <?php
            endforeach;
            wp_reset_postdata();
            ?>
        </div>
    <?php endif; ?>
</div>
1
  • i have two question; 1- how i can control the length of excerpt? & 2- why the order of child pages is based on alphabet or numbering? how i can fix this?
    – iman
    Apr 22, 2018 at 19:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.