Multimedia and Web Design Firm
Flash replacement

Excluding current post / url in WordPress Loop

May 30th, 2009

A very quick post today but hopefully this will help a few people.

WordPress is a great tool for a small website requiring regular editing. While using the loop i wanted to find a way to exclude the current pages url from a list of links. There is no point in a page linking to itself!

By using the the post__not_in function its easy to do.

<?php query_posts(array('showposts'=> 6,'post__not_in' => array($post->ID)));
while (have_posts()) : the_post(); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endwhile;?>

Did it work? Any problems? Post a comment below.

Telephone:

+44 (0)1224 582948

Office Address:

60 Nelson Street,
Aberdeen,
Scotland,
AB24 5ES