Skip to content

Instantly share code, notes, and snippets.

@carlosonweb
Last active October 30, 2017 20:25
Show Gist options
  • Save carlosonweb/47910965cd589b14b183c7d8dc08a5dd to your computer and use it in GitHub Desktop.
Save carlosonweb/47910965cd589b14b183c7d8dc08a5dd to your computer and use it in GitHub Desktop.
A shortcode to display the child pages of a parent page.
<?php
/**
* A shortcode to display the child pages of a parent page.
*
* Usage: Simply embed [show_childpages_on_parent] on the parent page.
*
*/
add_shortcode('show_childpages_on_parent', function(){
global $post;
$html = '';
$child_pages = get_pages( array(
'child_of' => $post->ID,
'sort_column' => 'post_date',
'sort_order' => 'desc'
));
foreach( $child_pages as $page ) {
$id = $page->ID;
$content = $page->post_content;
$title = $page->post_title;
$excerpt = $page->post_excerpt;
$permalink = get_permalink( $id );
$thumbnail = '';
if ( has_post_thumbnail( $id ) ){
$thumbnail = '<div>' . get_the_post_thumbnail( $id, 'thumbnail', array( 'class' => 'alignleft' ) ) . '</div>';
}
if ( !$excerpt ){
$excerpt = '<div>' . $thumbnail . wp_trim_words( $page->post_content ) . '</div>';
}
$html .= '<div class="entry child-page-on-parent" style="clear:both"><h2><a href="' . $permalink . '">'. $title . '</a></h2>' . $excerpt . '</div>';
}
return $html;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment