Skip to content

Instantly share code, notes, and snippets.

@dankoch-cz
Created March 9, 2022 15:50
Show Gist options
  • Select an option

  • Save dankoch-cz/30ffc92776b75789706a0796e9fa8d0c to your computer and use it in GitHub Desktop.

Select an option

Save dankoch-cz/30ffc92776b75789706a0796e9fa8d0c to your computer and use it in GitHub Desktop.
WP shortcode for listing pages
<?php
function list_pages_func( $atts ) {
$a = shortcode_atts( array(
'type' => 'pages',
'id' => '',
), $atts );
$args = array(
'post_type' => $a['pages'],
'posts_per_page' => -1
);
if(isset($a['id']) && $a['id']) {
$args['post_parent'] => $a['id'];
}
$buffer = '';
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
$buffer .= '<ul>';
while ( $the_query->have_posts() ) : $the_query->the_post()
$buffer .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
endwhile;
endif;
wp_reset_post_data();
return $buffer;
}
add_shortcode( 'list_pages', 'list_pages_func' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment