• Resolved martinconnelly

    (@martinconnelly)


    Hi Guys,
    I’ve been bashing my head against my desk all afternoon trying to figure this out. I’ve got a custom post type all set up, with hierarchical set to “true” and I can assign parents and see the relationship in the back end. Which is great.

    Except I want to list the children (and siblings) of my special new post type. I found this code

    <?php
    if($post->post_parent)
    	$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
    else
    	$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
    if ($children) { ?>
    	<ul>
    		<?php echo $children; ?>
    	</ul>
    <?php } ?>

    which works perfectly when I put it into a page template for pages. But it returns squat for the custom post template.
    I thought “$post->post_parent)” might be the issue — but it’s not:

    <h1 class="page-title"><a href="<?php echo get_permalink($post->post_parent) ?>" title="<?php printf( __( 'Return to %s', 'your-theme' ), wp_specialchars( get_the_title($post->post_parent), 1 ) ) ?>" rev="attachment"><span class="meta-nav">&laquo; </span><?php echo get_the_title($post->post_parent) ?></a></h1>

    (lifted from an attachment template) does give me a back link to the parent. — so maybe it has something to do with wp_list_pages? Maybe it’s something else? I’d appreciate any ideas.

    Thanks in advance,

    Martin

Viewing 5 replies - 1 through 5 (of 5 total)
  • https://codex.www.ads-software.com/Function_Reference/wp_list_pages#List_members_of_a_custom_post_type

    may help you….. I have never even played with it, so just trying to point something helpful out…

    But looks like you need to specify the custom post type to wp_list_pages

    Thread Starter martinconnelly

    (@martinconnelly)

    Hi Rev. I found that a while back, but near as I can see that’s how you could list all the pages in a certain post type…which seems useful, but i’m not sure it helps. the kicker is that $post->post_parent definitely works without assigning a specific post category — which means that $children *should* work too…

    Thanks for the tip, though.

    -m

    Thread Starter martinconnelly

    (@martinconnelly)

    Solved, with huge help from Rarst over at https://wordpress.stackexchange.com/

    All you need to do is add $post_type=whatever to the line
    $children = wp_list_pages..

    so it looks like

    $children = wp_list_pages(“title_li=&child_of=”.$post->post_parent.”&echo=0$post_type=whatever”);

    phew.

    -m

    JsonB123

    (@jsonb123)

    You have a typo in there, I believe your code should be:

    $children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0&post_type=whatever");

    I replaced the ‘$’ with ‘&’.

    Also, I just used a variation of this to just simply print out all of my custom post of a certain type via:

    wp_list_pages("title_li=&post_type=custom_post_type_name");

    I hope that this helps someone!

    Thank you, JsonB123!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘wp_list_pages and hierarchical custom post type’ is closed to new replies.