RBA Cash Rate: 4.35% · 1AUD = 0.67 USD · Inflation: 4.1%  
Leading Digital Marketing Experts | 1300 235 433 | Aggregation Enquires Welcome | Book Appointment
Example Interest Rates: Home Loan Variable: 5.20% (5.24%*) • Home Loan Fixed: 5.48% (6.24%*) • Fixed: 5.48% (6.24%*) • Variable: 5.20% (5.24%*) • Investment IO: 5.78% (6.81%*) • Investment PI: 5.49% (6.32%*)

Display a List of Child Pages With WordPress Shortcode

Display a List of Child Pages With WordPress Shortcode

When you publish a page in WordPress, you have the option of selecting a parent page. All pages underneath that page become child pages - essentially creating a subset of pages (attached to a topic). The WordPress shortcode and plugin as described in this article will permit you to render all page results attached to a parent page in a list. See examples on our code page and reference page. While we've provided a few of these types of shortcode in the past, they'll all be rewritten so the code is a little closer to WordPress best practice.

The Result

As linked to above, the shortcode will list pages attached to a parent page (or a page as defined in your shortcode). As an example, the shortcode of [childpages page="12345" description="false"] will return the list as rendered below (where '12345' is your parent page). If the page attribute is not used, the current page will be used as the parent page. I've used description="0" to prevent the excerpt from printing.

Shortcode Attributes

The following shortcode attributes may be used. We'll add more options in the future so make sure you subscribe to us on Facebook for updates.

page

By default, the current page will be used. To list the child pages of another page, use page="12345" where 12345 is that page ID.

remove

The remove attribute is used to remove a string of text to be omitted from your excerpt. In our case, we use it to remove the tweet text and Facebook that's rendered by default with our post. So, we use remove="Tweet  and like us on FB ".

description

The excerpt will print by default. To silence it, use description="0".

words

The words attribute relates to the number of words in your excerpt. To limit your excerpt to 20 words, use words="20".

exclude

To omit certain pages from your results, use exclude="20,21,22,23" (a comma-delimited list of page IDs to be left out).

WordPress Shortcode

Copy and paste the WordPress function into your theme's functions.php file or, if you sensibly have one installed, your custom functions plugin.

1
<?php 
2
/*
3
 Display a List of Child Pages With WordPress Shortcode
4
 http://www.beliefmedia.com/display-child-pages-wordpress
5
*/
6
 
7
function beliefmedia_child_pages($atts) {
8
 
9
  $atts = shortcode_atts(array(
10
    'page' => false,
11
    'remove' => '',
12
    'description' => true,
13
    'words' => 60,
14
    'exclude' => false,
15
    'number' => false,
16
  ), $atts);
17
 
18
    global $post;
19
    $page = ($atts['page'] !== false) ? $atts['page'] : $post->ID;
20
 
21
    /* Excluded posts */
22
    $exclude = ($atts['exclude'] != false) ? $exclude = explode(',', $atts['exclude']) : array();
23
 
24
    $args = array(
25
        'post_parent' => $page,
26
        'post_type' => 'page',
27
        'post__not_in' => $exclude
28
    );
29
 
30
    /* Limit number of posts? (for pagination purposes) */
31
    $args['posts_per_page'] = ($atts['number'] !== false) ? $atts['number'] : '-1';
32
 
33
    $subpages = new WP_query($args);
34
 
35
    /* Build list of pages */
36
    if ($subpages->have_posts()) :
37
 
38
        $output = '<ul>';
39
 
40
        while ($subpages->have_posts()) : $subpages->the_post();
41
 
42
            if ( ($atts['remove'] != '') && ($atts['description']) ) $excerpt = trim(str_replace($atts['remove'], '', get_the_excerpt()));
43
            if ( ($atts['words'] !== false) && ($atts['description']) ) $excerpt = wp_trim_words($excerpt, $num_words = $atts['words'], $more = null );
44
            $output .= '<li><strong><a href=&quot;' . get_permalink() . '&quot; title=&quot;' . get_the_title() . '&quot;>' . get_the_title() . '</a></strong>';
45
            if ($atts['description']) $output .= '<p>' . $excerpt  . ' [<a href=&quot;' . get_permalink() . '&quot; title=&quot;' . get_the_title() . '&quot;>more</a>]</p>';
46
            $output .= '</li>';
47
 
48
        endwhile;
49
        $output .= '</ul>';
50
 
51
       /* Reset query */
52
       wp_reset_postdata();
53
 
54
    else :
55
 
56
        $output = '<p>No pages found.</p>';
57
 
58
    endif;
59
 
60
 return $output;
61
}
62
add_shortcode('childpages', 'beliefmedia_child_pages');

If you require shortcode to work in a sidebar widget, you'll have to enable the functionality with a filter. If you're using our custom functions plugin, you'll have that feature enabled by default.

Considerations

  • Read more on wp_query here .
  • We'll likely develop this into a more robust plugin with pagination and other features. So, the plugin option isn't entirely unreasonable (we'll let you know when the update is available).

Download


Title: Display a List of Child Pages With WordPress Shortcode
Description: Display a List of Child Pages With WordPress Shortcode.
  Download • Version 0.2, 1.8K, zip, Category: WordPress Plugins (General)
WordPress Shortcodes, (1.2K)    

Download our 650-page guide on Finance Marketing. We'll show you exactly how we generate Billions in volume for our clients.

  E. Australia Standard Time [ UTC+10, Default ] [ CHECK TO CHANGE ]

  Want to have a chat?
 

RELATED READING

Like this article?

Share on Facebook
Share on Twitter
Share on Linkdin
Share on Pinterest

Leave a comment