Created
March 9, 2022 15:50
-
-
Save dankoch-cz/30ffc92776b75789706a0796e9fa8d0c to your computer and use it in GitHub Desktop.
WP shortcode for listing pages
This file contains hidden or 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 | |
| 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