new WP_Query function
-
I have a function to get related posts based on tags.
function getRelatedPosts($pid) { # for use in the loop, list 5 post titles related to first tag on current post $tags = wp_get_post_tags($pid); if ($tags) { echo '<h3>Related Posts</h3>'; $first_tag = $tags[0]->term_id; $args=array( 'tag__in' => array($first_tag), 'post__not_in' => array($post->ID), 'showposts'=>5, 'caller_get_posts'=>1 ); $my_query = new WP_Query($args); if( $my_query->have_posts() ) { while ($my_query->have_posts()) : $my_query->the_post(); ?> <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p><?php endwhile; } } else print '<p>No related posts</p>'; }
In single.php I am calling it.
<p class="tags"><?php the_tags('<strong>Tagged:</strong> ', ', ', ''); ?></p> <div id="related_posts"><?php getRelatedPosts($post->ID); ?></div> <?php comments_template(); ?>
It works great, but then the comments template thinks the current post is whatever the last result returned from getRelatedPosts() is, rather than the actual post. Anything after it actually.
Any ideas?
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘new WP_Query function’ is closed to new replies.