When trying to output a function\'s content, you have to notice whether you want to pass the data to another function (or something else which you want to feed), or you want to directly print it to the browser.
If you use return
, your function will return the data, so you can use them in a secondary function as below:
second_function(first_function($input));
If you want to simply print the content to the browser, use either echo
or print_r
instead of return
. It\'s recommended to use echo
in your case. However, do not use echo
while making shortcode functions. It will output the text in the ways you don\'t want to.
Back to our WordPress problem, shall we?
For the provided structure, use the following function:
function wpb_list_child_pages() {
global $post;
if ( is_page() && $post->post_parent )
$childpages = wp_list_pages( \'sort_column=menu_order&title_li=&child_of=\' . $post->post_parent . \'&echo=0\' );
else
$childpages = wp_list_pages( \'sort_column=menu_order&title_li=&child_of=\' . $post->ID . \'&echo=0\' );
if ( $childpages ) {
$string = \'
<nav class="page-nav">
<h3>Navigation Title</h3>
<ul>
<li><a href="\'.get_permalink($post->post_parent).\'">\'.get_the_title($post->post_parent).\'</a></li>\'
.$childpages.
\'</ul>
</nav>\';
}
return $string;
}
add_shortcode(\'wpb_childpages\', \'wpb_list_child_pages\');
Now, use this function to output your menu to wherever you wish:
<?php echo wpb_list_child_pages(); ?>
or do the shortcode:
echo do_shortcode( \' [wpb_childpages] \' );
or even use the shortcode in a text widget:
[wpb_childpages]
All producing the same result.