Featured Content For WordPress
It’s surprisingly easy to build a neat little featured content area in WordPress, using a custom query to grab a couple of posts from a selected category, tag or even custom field. In this post we’ll find out how to build a simple featured content area for your blog, rather like the one I recently added to WPShout:
Choose your posts
First thing you need to do is choose which posts you want to have in your featured area. For argument’s sake, I’m going to use a category “featured”. We’re going to be building something like what’s currently on WPShout: no fancy jQuery, just something smart and functional that clearly shows off a couple of posts. Because we’re showing only three posts, we’re not going to do the usual and display the three most recent posts, but be bold and randomly pick three posts out of our category “featured”. This requires a custom query looking something like this:
<?php
$featured = new WP_Query();
$featured->query('posts_per_page=3&category_name=Featured&orderby=rand');
?>
<?php while ($featured->have_posts()) : $featured->the_post(); ?>
<?php endwhile; ?>
Displaying posts
Now that we’ve got our posts, we need to display them. We’ll wrap them in a div with the titles wrapped in H2s and linked up, followed by the post’s excerpt. That’ll all end up something like this:
<div class="featured-post">
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php the_excerpt(); ?>
</div><!-- .featured-post -->
Of course, we’ll then need toย sandwichย that in our query, so we end up with the following:
<?php
$featured = new WP_Query();
$featured->query('posts_per_page=3&category_name=Featured&orderby=rand');
?>
<?php while ($featured->have_posts()) : $featured->the_post(); ?>
<div class="featured-post">
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php the_excerpt(); ?>
</div><!-- .featured-post -->
<?php endwhile; ?>
Styling the featured content
Next a little styling. Nettus recently published a screencast showing how to make the fold over (that’s actually a triangle) effect purely with CSS. We’ll be using that technique here to give a little pop to our featured content. We’ll also be employing some subtle CSS3 shadows and gradients to subtly make our content look awesome.
First, we need to wrap the whole lot in a div and give that a width, background etc:
#welcome{ position:relative; margin:0; background: #EAEAEA;
height:180px; padding:20px 20px 20px 40px; width:662px;
text-shadow:1px 1px 1px #fff; margin:0 0 0 -20px;
-webkit-box-shadow: 1px 1px 3px #292929;
-moz-box-shadow: 1px 1px 3px #292929; }
In addition to the CSS3 box shadow we’ve got, you might like to add some CSS3 gradients to the box, something like this looks subtly awesome!
background: -webkit-gradient(linear,left top,left bottom,from(#fff),to(#C9C9C9),color-stop(1,#C9C9C9)) fixed no-repeat 0 100%;
background: -moz-linear-gradient(100% 100% 90deg, #C9C9C9, #FFF) fixed no-repeat 0 100%;
We can then add a little styling to the .welcome-post class we created to wrap each post with:
.welcome-post { width:200px; padding:0 20px 0 0; float:left;}
This’ll split up the area into three little columns, one for each post.
We can then style links and H2s:
#welcome h2{ font-family:arial; border-bottom:dashed 1px #aaa; padding: 0 0 10px; }
#welcome a:hover{ color:#444; }
And again, some optional CSS3 enhancement:
#welcome a{ -webkit-transition: all 0.2s ease-in-out; -moz-transition: all 0.2s ease-in-out; }
Finally, we just need to add the little arrow:
.arrow { border-left: 20px solid transparent;
border-top: 10px solid #3E8AAD; height: 0px; left: 0px;
line-height: 0; position: absolute; top: 221px; width: 0px; }
And we’ve created an awesome featured content area. You can grab the source files below and just check out the homepage for an example.
[button href=https://wpshout.com/downloads/featured-area.zip]Download[/button]
I’m using Custom Taxonomies (as opposed to categories) to organise a site. I use Stickies to feature posts (as it seems a reasonably-simply solution). However I want my sticky/feature posts to appear at the top of EVERY page (all taxonomies – regardless).
Apart from manually assigning each sticky post to every taxonomy, is there some way to achieve this?
Why do you use postion relative twice ๐ ?
Because I’m an idiot. Ta for the heads up ๐
I tried it but didn’t notice that too! Not until I read Furuskog’s comment. ๐
It wouldn’t have had an effect on the functionality, just wasting load time very slightly.
[…] Build A WordPress Powered Featured Content Area […]
I saw the added intro box in the homepage last week, noticed that you didn’t use any image, just pure CSS. Was tad shy to ask how you did it, but here it is now, complete with source files! ๐
Downloaded and tested it already, works perfectly! I just changed the “featured-post” class to “welcome-post”.
Thanks a lot for sharing!
Yes, yes! WordPress to the rescue. Thanks for the heads up in this technique. Looking to get more traffic, this could be a solution!
It’s a fantastic way of keeping readers on your site. Let me know how it goes!
Hello,
Nice tutorial although I would suggest not creating a category for featured and instead using the commonly unused sticky feature built into WordPress. This is much more logical for both the content and structure of WordPress.
Just assign a post to sticky and then change the query to.
$featured->query('posts_per_page=3&post__in=' . get_option('sticky_posts') . ',
&orderby=rand');
Dave.
Not sure about that. Sticky posts are meant for holding posts at the top of your site and whilst they’re good when you want to have just a single post featured, in this situation I don’t think they’re the best option as potentially posts are going to be in the featured content area for a couple of months, as I’ve got it set up on Shout. This’ll be a pain to get around in archives and even when just running the standard loop below — as no single post stands out, stickies will get pushed to the top so you’ll have to filter them out, but what if you wanted them just in order?! It can be done, just it’s a pain! This makes things simpler.
Actually, I agree with you re: sticky. I would actually use a tag as opposed to a category myself, but as you noted, you used a category simply for the sake of this tut.
I do see the flexibility in using tags – that will make thing a whole lot easier. I am currently building a featured section for a charity website. This featured section tutorial could easily be improved by allowing the post-thumbnail feature:
<div class="featured-post">
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
?>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php the_excerpt(); ?>
</div><!-- .featured-post โ>
Check http://bit.ly/7RnaCm for a better guide to the post-thumbnails feature
P.S.- The code you used for the commenting should be <!– and not <!-
Actually you can make the thumbnail function better by restricting the width to the same size as .welcome-post
the_post_thumbnail(array(200,9999));
9999 stands for any random height.