1

I am attempting to display the child pages of a custom post_type page. I found a shortcode that works great for standard WordPress "pages" post_type, but doesn't work on custom post types.

Here is the shortcode code I placed in my overrides functions file:

add_shortcode( 'my_childpages', 'my_list_child_pages' );  

function my_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 = '<ul>' . $childpages . '</ul>';
}
return $string; 
}

If I place the [my_childpages] shortcode on a "page" post_type with children it successfully displays in linked list all sub-pages. But if I add to a custom post_type (e.g., 'essays'), it doesn't.

I saw elsewhere that perhaps the code doesn't acknowledge the custom $post_type, and requires an added argument, but I'm not sure how to edit the above code to satisfy. I've tried several edits, but fail.

Any advice or suggestions would be greatly appreciated!

0

The page itself is a custom post type, you can notice this when you open the edit page screen. Your URL will be like this:

www.example.com/wp-admin/edit.php?post_type=page

This means you are on a page post type.

Now about your code, the first condition of your conditional determines if you are on a page post type or not. In the else section, you can also set the post_type as an argument:

$childpages = wp_list_pages( 'post_type=essay&sort_column=menu_order&title_li=&child_of=' . $post->ID . '&echo=0' );

However, you have to have the hierarchical set to true while registering your custom taxonomy:

'hierarchical'=> true,

Take a look at this question, which has some valuable information about this. The related page from the code reference can also be useful.

  • Works beautifully! 2 follow-up questions: (1) is there a way to list multiple post types as arg? e.g., could I add additional as comma separated list, or perhaps by adding multiple 'post_type=cpt1&post_type=cpt2&post_type=cpt3...' (2) so basically, these are arguments with '&' instead of ',' as syntax? – Benjamin Doyle Jul 29 '17 at 5:16
  • 1) The arguments themselves can be either array or string. However, the post_type argument accepts only a string, which means it can only take a single value. Besides, have you thought about how will it be rendered if you use multiple post types? 2) If you use the string method, then yes you should use &. But if you pass an array, you should use , instead. – Jack Johansson Jul 29 '17 at 6:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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