0

I am trying to display the sub-pages of a custom page on the parent page. I found an abbreviated code that works very well for standard WordPress post_type "pages", but does not work for custom publishing types.

Here's the short code code I put in my functions.php file:

function v_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;

}

add_shortcode('v_childpages', 'v_list_child_pages');

If I place the shortcode[v_childpages] on a post_type "page" with children, it successfully displays all subpages in the linked list. But if I add it in a custom post_type, it's not like that.

Any advice or suggestion is welcome, thank you!

2 Answers 2

0

The third line of your code specifies the page post type by checking if is_page(). You can change that to is_singular('your-post-type') so that it runs, then in your call to wp_list_pages() you also need to add a post_type argument so that that part of the code knows what to look for.

So, to have this shortcode work on both Pages and your specific CPT, sub in your post type's registered name in both places below where uve_courses appears:

function v_list_child_pages() { 
    global $post;
    // for Pages with a parent
    if ( is_page() && $post->post_parent ) {
        $childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->post_parent . '&echo=0' );
    // for individual CPTs with a parent
    } elseif(is_singular('uve_courses') && $post->post_parent ) {
        $childpages = wp_list_pages( 'post_type=uve_courses&sort_column=menu_order&title_li=&child_of=' . $post->ID . '&echo=0' );
    }
    if ( $childpages ) {
        $string = '<ul>' . $childpages . '</ul>';
    }
    return $string;
}
add_shortcode('v_childpages', 'v_list_child_pages');

If you have additional CPTs you want this to work on, you will need to add additional else clauses to pass those post types.

8
  • Thank you very much for the answer, I have applied the changes you recommended but I still don't get anything out of it... I did the above guided by this article: link Commented May 17, 2018 at 14:50
  • What is the name of your custom post type? This code depends on having the correct (usually lowercase) post type in 2 places - one in is_singular and one in the wp_list_pages line right below it.
    – WebElaine
    Commented May 17, 2018 at 15:27
  • I have put the name of my custom post type, which is the one that appears in the url: localhost/wordpressv/wp-admin/edit.php?post_type=uve_courses Commented May 17, 2018 at 15:49
  • Updated the answer above - there are 2 places to sub in your uve_courses post type. While you're making edits, it's often helpful to make a CPT name singular - like uve_course - so that in certain functions you can tell whether you're dealing with a single post or group of posts.
    – WebElaine
    Commented May 17, 2018 at 15:54
  • You may also want to remove sort_column=menu_order from the uve_courses conditional if your post type doesn't have a menu_order... which I suspect is the case.
    – WebElaine
    Commented May 17, 2018 at 15:55
0

I was finally able to solve the problem I was having.

In the else section I set post_type as an argument:

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

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.