Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Create a short code that list child pages of active page WordPress

  • SOLVED

I've recently began looking at using short codes on a current project, and am running into an issue that I need to get resolved quickly.

<strong>The question</strong>: <em>How do I create a short code that list child pages of active page?</em>

<strong>The Scope</strong>: Create a short code function that can be called from within the WYSIWYG wp editor on a page, that lists the sub/child pages for that active page. It should display them as links, or better yet an unordered list of links to the pages.

I've referenced both of these:

[[LINK href="http://codex.wordpress.org/Function_Reference/get_pages"]]Get Pages Function[[/LINK]]
[[LINK href="http://codex.wordpress.org/Template_Tags/wp_list_pages"]]List Pages Function[[/LINK]]

But neither seem to do what I am looking for, please help.

Answers (2)

2010-03-10

Ben Huson answers:

There's also this one:
[[LINK href="http://wordpress.org/extend/plugins/list-pages-shortcode/"]]http://wordpress.org/extend/plugins/list-pages-shortcode/[[/LINK]]


Aaron comments:

Sorry, forgot to mention the solution shouldnt be a third-party plugin... looking for a piece of code :)

Thanks


Ben Huson comments:

In theory those plugins should be GPL licensed which means the code is available for you use and modify. The code is actually very simple just download the List Pages Shortcode plugin, take a look and you'll see.


Ben Huson comments:

In it's most basic form, the code should look like this:

function shortcode_child_pages( $atts, $content ) {

global $post;

$output = wp_list_pages( array( 'child_of' => $post->ID ) );
if ( !empty( $output ) ) {
$output = '<ul class="child-pages">' . $output . '</ul>';
}

return $output;

}

add_shortcode( 'childpages', 'shortcode_child_pages' );


Then use the shortcode [childpages]


Aaron comments:

This is more along the lines of what I'm looking for but without it displaying the heading 'Pages' and why does it place the links at the top of the post content even if I'm calling the short tag in the middle of the content in the WYSIWYG?


Ben Huson comments:

Just change the parameters for wp_list_pages:

$output = wp_list_pages( array( 'child_of' => $post->ID, 'title_li' => '', 'echo' => 0 ) );


Aaron comments:

Thanks!

2010-03-10

Colin Pool answers:

I think this plugin is what you need: [[LINK href="http://wordpress.org/extend/plugins/wordpress-navigation-list-plugin-navt/"]]http://wordpress.org/extend/plugins/wordpress-navigation-list-plugin-navt/[[/LINK]]

It makes custom navigation lists. You can change everything you want in the css and you can select the paged you want to show each list.


Aaron comments:

Sorry, forgot to mention the solution shouldnt be a third-party plugin... looking for a piece of code :)

Thanks


Colin Pool comments:

<blockquote>
First you need to enable PHP in you're pages before you can put PHP code into pages with the editor.
You can enable this with a plugin.

Then you put this code in the html editor of the page:


<ul>
<?php
global $id;
wp_list_pages("title_li=&child_of=$id&show_date=modified
&date_format=$date_format"); ?>
</ul>


If I get you right then this is the code you need.
</blockquote>