Created
          April 7, 2015 18:31 
        
      - 
      
- 
        Save thierrypigot/52a8d8c17cda44333e62 to your computer and use it in GitHub Desktop. 
    List all children of current item (page, custom post type ...) #WordPress
  
        
  
    
      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 | |
| /** | |
| * Shortcode Custom post type children as list | |
| * Thierry Pigot | |
| * 2015-04-07 | |
| * | |
| * [children id='ID' nb='-1' type='page' depth='1'] | |
| **/ | |
| function tp_shortcode_children( $atts ) | |
| { | |
| global $post; | |
| extract( shortcode_atts( array( | |
| 'id' => $post->ID, | |
| 'nb' => -1, | |
| 'type' => 'page', | |
| 'depth' => '1', | |
| 'orderby' => 'title', | |
| 'order' => 'ASC', | |
| ), $atts ) ); | |
| $params = array( | |
| 'post_type' => $type, | |
| 'posts_per_page' => $nb, | |
| 'post_parent' => $id, | |
| 'depth' => $depth, | |
| 'orderby' => $orderby, | |
| 'order' => $order | |
| ); | |
| $children = query_posts( $params ); | |
| if( count( $children ) > 0 ) | |
| { | |
| $html = '<ul id="tp-'. $type .'-'. $id .'-children" class="tp-'. $type .'-children">'; | |
| foreach( $children as $child ) | |
| { | |
| $html .= '<li><a href="'. get_page_link( $child->ID ) .'" title="'. $child->post_title .'">'. $child->post_title .'</a></li>'; | |
| } | |
| $html .= '</ul>'; | |
| return $html; | |
| } | |
| return false; | |
| } | |
| add_shortcode( 'children', 'tp_shortcode_children' ); | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment