0

This has only been a problem since I updated to Wordpress 3.1

I have a page that displays a list of custom post types. The page is the child of a parent page. There is a sidebar that lists sibling pages. The code for the two is conflicting somehow, and I can't get the list of pages to show up.

This is the code to display the list of custom post type (in my case 'sponsor'):

<?php
$args = array( 'numberposts' => 30, 'post_type' => 'sponsor' );
$sponsors = get_posts( $args );
foreach ($sponsors as $post) :  setup_postdata($post); 
?>

<div class="content">
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
</div>

<?php endforeach; ?>

This is the code in my sidebar to list the sibling pages:

<?php
    if (!$post->post_parent) {
        $children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');
    } else {
        $p = end(get_post_ancestors($post->page));
        $children = wp_list_pages('title_li=&child_of='.$p.'&echo=0');
    }
?>
<?php
    if ($children) {
        $parent_title = end(get_post_ancestors($post));
?>
    <ul>
    <?php echo $children; ?>
<?php } ?>
    </ul>

Nothing is showing up in the sidebar. I tried deleting the code for the posts, and the list of pages showed up correctly. But something about the "get_posts" is not letting the "list_pages" work.

Anyone know what alternate code I can use?

3
  • 1
    Tried a reset after the get_posts call? eg. wp_reset_query, it's suppose to clear up variables used by template tags inside post loops.
    – t31os
    Commented Feb 24, 2011 at 17:49
  • Yeah, that seemed to work. Probably something I should know, but still a newbie. Thank you!
    – user3122
    Commented Feb 24, 2011 at 21:08
  • @t31os Add your solution as an answer … so we can upvote it and get this question out of the list of unanswered questions.
    – fuxia
    Commented May 1, 2012 at 21:05

2 Answers 2

1

From comment:

Use wp_reset_query() after each loop to undo the changes made to global variables by setup_postdata().

1

From what I can see, it might be that you don't have the 'post_type' => 'custom-post-type-name' set in your wp_list_pages arguments. I know that mine doesn't show anything if I miss that out. You do have it in your get_posts args, which might be why that was working. Try this in your sidebar:

<?php
    if (!$post->post_parent) {
        $children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0&post_type=sponsor');
    } else {
        $p = end(get_post_ancestors($post->page));
        $children = wp_list_pages('title_li=&child_of='.$p.'&echo=0&post_type=sponsor');
    }
?>
<?php
    if ($children) {
        $parent_title = end(get_post_ancestors($post));
?>
    <ul>
       <?php echo $children; ?>
    </ul>
<?php } ?>

Although I could of course just be barking up the wrong tree if wp_reset_query () makes it work fine for you.

Also, this wouldn't help if the pages you want wp_list_pages to reference aren't custom post types, it just wasn't clear from your question whether they were or not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.