Created
February 14, 2018 19:09
-
-
Save douglaskarr/68519751a4541cbb469bb9a00da94fa1 to your computer and use it in GitHub Desktop.
WordPress: Register a shortcode to list child pages on a parent page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<php | |
// Turn on excerpt support for pages by enabling them in your themes functions.php file | |
add_post_type_support( 'page', 'excerpt' ); | |
/* Add the below function to your theme's functions.php file | |
** This has more capabilities than get_pages and wp_list_pages | |
** Usage: [list_subpages aclass="button" ifempty="Sorry, we currently don't have any job openings."]<h3>List of Jobs</h3>[/list_subpages] | |
*/ | |
// List Subpages in a List | |
function dk_list_child_pages( $atts, $content = "" ) { | |
global $post; | |
$atts = shortcode_atts( array( | |
'ifempty' => 'No Records', | |
'aclass' => '' | |
), $atts, 'list_subpages' ); | |
$args = array( | |
'post_type' => 'page', | |
'posts_per_page' => -1, | |
'post_parent' => $post->ID, | |
'orderby' => 'publish_date', | |
'order' => 'DESC', | |
); | |
$parent = new WP_Query( $args ); | |
if ( $parent->have_posts() ) { | |
$string .= $content.'<ul>'; | |
while ( $parent->have_posts() ) : $parent->the_post(); | |
$string .= '<li><a class="'.$atts['aclass'].'" href="'.get_permalink().'" title="'.get_the_title().'">'.get_the_title().'</a>'; | |
if ( has_excerpt( $post->ID ) ) { | |
$string .= ' - '.get_the_excerpt(); | |
} | |
$string .= '</li>'; | |
endwhile; | |
} else { | |
$string = '<p>'.$atts['ifempty'].'</p>'; | |
} | |
wp_reset_postdata(); | |
return $string; | |
} | |
add_shortcode('list_subpages', 'dk_list_child_pages'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment