0815

Du musst dich anmelden um Beiträge und Themen zu erstellen. Einloggen · Registrieren

Display Related Posts without a Plugin

We don’t need no stinking plugins to display related posts! Here’s how to do the job using tags as the associative criteria.

Place the following code in your loop and take a nap or something:

<?php // related posts based on first tag of current post
$tags = wp_get_post_tags($post->ID);
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' => 7, // how many posts?
			'caller_get_posts' => 1
			);

	$my_query = new WP_Query($args);
  	if ($my_query->have_posts()) { ?>

		<ul>

		<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>

			<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>

		<?php endwhile; ?>

		</ul>

	<?php } ?>
<?php } ?>

Customize as needed. To change the number of posts, edit the line that says, “how many posts?”

Source: Enhanced version of the code provided by MichaelH (via WP Codex)




0