0

I found this code snippet that shows childpages only.

It works great, doing exactly what i intend it to do. I want to be able to place this around the site, but i cant place PHP inside the Rich-text editor. I want to convert it to a shortcode.

How do i convert this to a shortcode?

<?php
global $wp_query;
if( empty($wp_query->post->post_parent) ) {
$parent = $wp_query->post->ID;
} else {
$parent = $wp_query->post->post_parent;
} ?>
<?php if(wp_list_pages("title_li=&child_of=$parent&echo=0" )): ?>
<div>
<ul>
<?php wp_list_pages("title_li=&child_of=$parent" ); ?>
</ul>
</div>
<?php endif; ?>

1 Answer 1

0

Put this in your theme's functions.php, it will add the shortcode [child_pages]:

function wpsc_child_pages($atts){
    global $wp_query;
    $html = '';
    if( empty($wp_query->post->post_parent) ) {
        $parent = $wp_query->post->ID;
    } else {
        $parent = $wp_query->post->post_parent;
    }
    $pages = wp_list_pages("title_li=&child_of=$parent&echo=0");
    if( $pages ){
        $html .= "<div><ul>$pages</ul></div>";
    }
    return $html;        
}

add_shortcode('child_pages', 'wpsc_child_pages');

If you want to pass the function variables (e.g. [child_pages parent=3]) you would access that variable using the passed $atts argument.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.