Posts Tagged ‘snippet’

WordPress – Featured Articles with Excerpt and Read More Link

Saturday, August 15th, 2009

I’ve been rather busy lately, mostly with web work, so I’ve been manipulating the WordPress template tags with increasing frequency. I often come across an issue that I can’t find the answer to as quick as I like, or in this case at all, so I’m sharing my little code developments with you. It is rather simple, and highly customizable, so use and update it to your heart’s content.

The below code creates, in WordPress, an unordered list of the 5 most recent posted items with your custom crafted excerpt followed by a read more link which can be changed as desired:

<h3>Recent Articles</h3>
        <ul><?php $postslist = get_posts('numberposts=5&order=DESC&orderby=date');
 		foreach ($postslist as $post) : 
 		?>
   	<li><a href="<?php the_permalink(); ?>">
              <?php the_title(); ?></a> - <?php the_excerpt(); ?> <a href="<?php the_permalink(); ?>" class="more">More &gt;&gt;</a>
        </li>
	<?php endforeach; ?>
    	</ul>

Here we have the same general code, but with this the there is a specific category it is pulling from, so you would change the “category=3” to your category’s id number:

<h3>Featured Articles</h3>
        <ul><?php $postslist = get_posts('numberposts=5&order=DESC&orderby=date&category=3');
 		foreach ($postslist as $post) : 
 		?>
   		<li><a href="<?php the_permalink(); ?>">
                      <?php the_title(); ?></a> - <?php the_excerpt(); ?> <a href="<?php the_permalink(); ?> "class="more">More &gt;&gt;</a></li>
		      <?php endforeach; ?>
    	</ul>

If you just want it to pull the first 55 characters from your entry, instead of a custom made excerpt, use the following:

<h3>Featured Articles</h3>
        <ul><?php $postslist = get_posts('numberposts=5&order=DESC&orderby=date&category=3');
 		foreach ($postslist as $post) : 
 		?>
   		<li><a href="<?php the_permalink(); ?>">
                      <?php the_title(); ?></a> - <?php the_content('<span class="more">More &gt;&gt;</span>'); ?></li>
		      <?php endforeach; ?>
    	</ul>

So, hopefully you WordPress users out there will find this useful. I’ll post a link to the site I’m currently using this on as an example, once the site is ready for public viewing.