Forums

[resolved] wp_list_pages and hierarchical custom post type (6 posts)

  1. martinconnelly
    Member
    Posted 1 year ago #

    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

  2. Rev. Voodoo
    Volunteer Moderator
    Posted 1 year ago #

    http://codex.wordpress.org/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

  3. martinconnelly
    Member
    Posted 1 year ago #

    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

  4. martinconnelly
    Member
    Posted 1 year ago #

    Solved, with huge help from Rarst over at http://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

  5. JsonB123
    Member
    Posted 1 year ago #

    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!

  6. @ ommunist
    Member
    Posted 8 months ago #

    Thank you, JsonB123!

Topic Closed

This topic has been closed to new replies.

About this Topic