the_excerpt() not showing manual excerpt for Pages

I have enabled the excerpt feature for pages in my wordpress theme –

add_action( 'init', 'my_add_excerpts_to_pages' );
    function my_add_excerpts_to_pages() {
         add_post_type_support( 'page', 'excerpt' );
    }

This has enabled the excerpt box when adding or updating pages through the admin area, which is what I expected.

I am then trying to display each excerpt on the homepage of my site like so –

$child_pages = $wpdb->get_results("SELECT *    FROM $wpdb->posts WHERE post_parent  = 64    AND post_type="page" ORDER BY post_title", 'OBJECT');    ?>
            <?php if ( $child_pages ) : foreach ( $child_pages as $pageChild ) : setup_postdata( $pageChild ); ?>
            <div class="memberHover" id="member-<?php echo $pageChild->ID; ?>">
              <div><h4><?php echo $pageChild->post_title; ?></h4>
                <p><?php the_excerpt(); ?></p>
              </div><?php echo get_the_post_thumbnail($pageChild->ID, '312,156'); ?>
            </div>
        <?php endforeach; endif; ?>

The issue is that even if I have a manual excerpt populated through the admin area, it still creates the auto excerpt when displaying on the homepage. It seems that it is not picking up the fact that there is a custom excerpt in the database for each item.

Any and all help is greatly appreciated!

Thanks,
Tristan

3 Answers
3

Because I am very particular about how to do things in WordPress when it comes to looping, I couldn’t help but want to post a better way to do the above loop in your question as seeing the way you’ve done it looks like a lot of unnecessary work for yourself.

You already answered your own question but for the sake of being able to use the function the_excerpt() I’ve rewritten the above code to work using the WP_Query object instead which I believe is a better way of going about loop related matters (not to mention easier). You really only need to use manual queries like that when you want to query something the WP_Query class cannot.

<?php
$args = array('post_parent' => 64, 'orderby' => 'title', 'order' => 'ASC', 'post_type' => 'page', 'post_status' => 'publish');
$child_pages = new WP_Query($args);
?>

<?php if ($child_pages->have_posts()): ?>

<?php while($child_pages->have_posts()): $child_pages->the_post(); ?>
            <div class="memberHover" id="member-<?php the_ID(); ?>">
              <div><h4><?php the_title(); ?></h4>
                <p><?php the_excerpt(); ?></p>
              </div><?php echo get_the_post_thumbnail($post->ID, '312,156'); ?>
            </div>
<?php endwhile; ?>

<?php endif; ?>

Leave a Comment