I am not sure why but I have used get_posts() to query for some data. Then I used setup_postdata() … I think its used so that I can use functions like the_permalink() etc with the new post data?

<?php foreach ($childPosts as $cp) : setup_postdata($cp); ?>

<article <?php post_class() ?> id="post-<?php the_ID(); ?>">
  <h1><a href="https://wordpress.stackexchange.com/questions/9834/<?php the_permalink() ?>"><?php the_title(); ?></a></h1>
  <?php if (has_post_thumbnail()) : ?>
  <a href="https://wordpress.stackexchange.com/questions/9834/<?php the_permalink() ?>"><?php the_post_thumbnail(($hasOutputNotFeaturedDiv) ? 'thumb-small' : null) ?></a>
  <?php endif; ?>
  <?php the_excerpt(); ?>
  <p class="more"><a href="https://wordpress.stackexchange.com/questions/9834/<?php the_permalink() ?>">Read more ...</a></p>
  <?php include (TEMPLATEPATH . '/inc/meta.php' ); ?>
</article>

<?php endforeach; ?>

but it appears that only the_excerpt contains the new post data value, why is that? I find that if I use echo get_the_permalink($cp) it works ok. But I think the shorter version will be better

6

I could be wrong, but from what I’m seeing, “setup_postdata()” should be used when doing a custom select query (not just query_posts):
http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query

As well, if you want to use tags like “the_title()” and “the_permalink()” with that custom select query … you’ll need to use the variable name $post specifically (not another variable name) in setup_postdata() – AS WELL – you should call global $post before your “foreach” loop…

So basically follow that example in that codex link. And don’t change the variable name $post – otherwise it breaks it.

HTH

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *