• gulliver

    (@gulliver)


    I have a hierarchical cpt, all entries for which have children.

    In a page of a parent, how do I list the children?

Viewing 8 replies - 1 through 8 (of 8 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    Use https://developer.wordpress.org/reference/functions/wp_list_pages/

    set the post_type to your CPT and child_of to the current page

    Thread Starter gulliver

    (@gulliver)

    Thanks.

    Struggling to understand this after too many hours staring at a screen, how do I ‘set child_of to the current page’?

    I assume it begins with ‘ ‘child_of’ =>’ but I can’t even guess the rest.

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    there are two possible ways:

    $post_id = get_the_ID();

    and

    global $post;
    $post_id = $post->ID;

    Here’s some code for a filter I created that automatically adds the children of a page called “services” after the page’s usual content

    /* for the services page, list children */
    
    function pcs2n_list_children( $content ) {
            global $post;
            if ( is_page( 'services' ) ) {
                    $args = array(
                    'child_of' => $post->ID,
                    'title_li' => '',
                    'echo' => false,
                            );
                                    $content .= wp_list_pages( $args );
            }
    
                    return $content;
    }
    add_filter( 'the_content', 'pcs2n_list_children' );
    Thread Starter gulliver

    (@gulliver)

    Thanks.

    I understand the ‘list children” snippet – and can adapt that to use on various pages.
    But I couldn’t adapt it to use on a cpt.

    I’m clearly failing to understand something which is probably quite straightforward… using my existing knowledge I can produce a page which lists all members of a cpt, but on a page for one member of that cpt I can’t show a list of children of that member.

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    In the above, you’d add ‘post_type’=> get_post_type() and it should list children for that particular post.

    Thread Starter gulliver

    (@gulliver)

    Steve thanks…

    Sometimes when looking at forum posts and seeing someone come back for clarification, I think ‘Jeez, you have all the clues there – it’s not that difficult.’

    Finding myself (again) in such a position, I think I should return to this later with a clear head… so then, let’s relax with ‘Box of Rain’ and think about anything other than WordPress. Maybe I’m just not suited to this. 😉

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    Maybe you’ll find direction
    around some corner
    where it’s been waiting to meet you

    Thread Starter gulliver

    (@gulliver)

    😉
    Hopefully, yeah.
    ‘I will get by… La dee da da da, La da da da da’.

    Waaaaaaay off topic, I should stop this nonsense.

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘Custom Post Type archive.’ is closed to new replies.