Skip to content

Instantly share code, notes, and snippets.

@joliveras
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joliveras/938cba1da36185807434 to your computer and use it in GitHub Desktop.
Save joliveras/938cba1da36185807434 to your computer and use it in GitHub Desktop.
Function for create a shortcode to display subpages
<?php function childpages_shortcode( $atts ) {
global $post;
$args = array(
'post_parent' => $post->ID,
'post_type' => 'page',
'depth' => 1,
'orderby' => 'menu_order',
'order' => 'ASC'
);
$childpages .= '<ul>';
$loop_subpag = new WP_Query($args);
if( $loop_subpag->have_posts() ):
while( $loop_subpag->have_posts() ): $loop_subpag->the_post();
$childpages .= '<li>' . get_the_title() . '</li>';
endwhile;
endif;
$childpages .= '</ul>';
wp_reset_postdata();
return $childpages;
}
add_shortcode( 'childpages', 'childpages_shortcode' ); ?>
@corey34
Copy link

corey34 commented Jul 17, 2014

How could this be edited so that in the shortcode, you can specify to only show pages that have a title that starts with an "A", "B", or "C"? Is this possible?

@joliveras
Copy link
Author

Hi can use conditionals inside the loop with the substr function http://php.net/manual/en/function.substr.php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment