• Content
  • Comments (0)
  • Related articles
Nov
05
2012

Get excerpt for child pages of parent page in WordPress Monday, 5 November 2012

Here is, polished WordPress page template code that displays all the child pages title, thumbnail, custom field and excerpt of the current parent page.
Note that this template includes piece of code that is Snilesh creation and  can be found here.

<?php
/*
Template Name: Page with [child pages title links & excerpt]
*/
?>

<?php get_header() ?>

<div id="wrapper">
<div id="container">
    <div id="content">
    <div id="post">

        <!-- display parent page title with link, edit link and the content is there is any -->
        <h1 class="entry-title">
            <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title() ?>"><?php echo get_the_title() ?></a>
            <span><?php edit_post_link('[Edit]', '', ''); ?></span>
        </h1>
        <div class="entry-content"><?php the_content() ?></div>

        <!-- extract from MySQL database the child pages -->
        <?php $child_pages = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_parent = ".$post->ID." AND post_type = 'page' ORDER BY menu_order", 'OBJECT'); ?>

        <!-- begin loop and display every child pages -->
        <?php if ( $child_pages ) : foreach ( $child_pages as $pageChild ) : setup_postdata( $pageChild ); ?>
            <!-- display child pages title with edit link -->
            <h2 class="entry-title">
                <a href="<?php the_permalink($pageChild->ID); ?>" rel="bookmark" title="<?php echo $pageChild->post_title; ?>"><?php echo $pageChild->post_title; ?></a>
                <span><?php edit_post_link('[Edit]', '', ''); ?></span>
            </h2>    
            <div class="page-excerpt">
                <!-- display custom field named [description] -->
                <span class="page-description"><?php echo get_post_meta($pageChild->ID, 'description', $single = true); ?></span>
                <!-- check for thumbnail, display page thumbnail with link or if there is none uses logo.png from theme /images folder -->
                <span class="page-thumbnail">
                    <?php
                    if ( has_post_thumbnail($pageChild->ID)) {
                        echo '<a href=' . get_permalink($pageChild->ID) . ' ' . 'title=' . $pageChild->post_title . '>' . get_the_post_thumbnail($pageChild->ID, array(270,100)) . '</a>';
                    } else { ?>
                        <a href="<?php bloginfo('url'); ?>" title="<?php bloginfo('name'); ?>"><img width="260" src="<?php bloginfo('template_url'); ?>/images/logo.png" /></a>
                    <?php } ?>
                </span>
                <!-- display child pages excerpt with [...] -->
                <span class="page-excerpt"><?php the_excerpt(); ?></span>
            </div><!-- .page-excerpt -->
        <?php endforeach; endif; ?>

    </div><!-- .post -->
    </div><!-- #content -->

    <?php get_sidebar() ?>
    <div></div>

</div><!-- #container -->
</div><!-- #wrapper -->

<?php get_footer() ?>

 

 

To stylise this template in current configuration you may need to add to CSS style file the following tags:

#wrapper {}
#container {}
#content {}
#post {}

h1.entry-title {}
h1.entry-title span {}

.entry-content {}

h2.entry-title {}
h2.entry-title span {}

.page-excerpt {}
.page-excerpt span {}
.page-excerpt span.page-description {}
.page-excerpt span.page-thumbnail {}
.page-excerpt span.page-excerpt {}

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*