A few days ago when I made some modifications to my blog’s template and added a featured posts box, I was asked how did I do that. It’s nothing too difficult or technical. All I did was installed the required Plugins, inserted the right code, and did a bit of styling to specify three of my favorite posts as featured.
There are a number of reasons why you might want to display a specific post on a specific part of your blog. You may want to display only one post on your blog homepage but still want to show the latest entries in the sidebar, or you may want to highlight the important posts, just like I did, to drive more visitors to those posts.
The following method gives you complete control over how, where, and which parts of your selected posts are shown.
So, without further ado, let’s do it!
Install required Plugins
Install Get-a-Post and Content Extract WordPress Plugins.
Get-a-Post displays a specific post (or Page) with standard WP template tags, while Content Extract provides PHP functions to provide extended extracts functionality.
You’ll use the combination of both to get a post title and post excerpt to appear wherever you want.
Insert the code
Insert the following code in your template file where you want the post to appear.
<?php get_a_post(31); ?>
Insert your post ID where it says ‘31′, but nothing is going to happen right now. You’ll have to insert the title template tag for the post title to appear.
<?php get_a_post(31); ?>
<h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
Now that you have your post’s title showing where you specified, it’s time to let the second Plugin take over.
<?php get_a_post(31); ?>
<h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
<?php wswwpx_content_extract ( 'Continue reading..', 20, 20 ); ?></div>
The string ‘Continue reading..’ makes up the link that takes you to the full post - which is placed at the end of the extract - so replace it with your own text.
The second parameter ‘20′ controls how many words to show before truncating the text, and this is the most interesting part. You can have it display one line, two lines or as many lines as you like. With so much control in your hand, you can make your posts easily fit in the sidebar or any other small area in your template. Plus, just copy the above code, and repeat it to display more posts. Remember to change the post ID so you won’t have the same post repeating many times.
This is it. You can style the whole thing as you like.
Just so you can see how I styled my featured posts box, here is the code.
<div id="featured"><div class="fbox"><?php get_a_post(31); ?>
<h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
<?php wswwpx_content_extract ( 'Continue reading..', 20, 20 ); ?></div>
(Repeated thrice with different post IDs)
And the CSS.
#featured{
width: 940px;
border-bottom: 1px solid #d4cbb4;
border-top: 3px solid #4a3705;
background: #fffdf6 url(images/featured.jpg) bottom right no-repeat;
margin: 12px 0;
padding-bottom: 8px;
float: left;
font-size: 90%;
}
.fbox{
width: 300px;
float: left;
margin: 8px 0 0 9px;
}
.fbox h1{
font-size: 130%;
}
.fbox a{
color: #823d00;
}
Please don’t use the above CSS code, as it is meant to be used only on my blog.
Side notes
- There is a WordPress function that fetches a post by ID, but it’s too complicated for my taste.
- You can also use the excerpt template tag, but it does not have any customizable parameters.
- If you specify the post excerpt in Write Post subpanel, it will override your specified word-count for truncation.
There may be other ways to achieve the same result without using any extra Plugins, so, if you know of such alternative methods, please let me know in the comments.
I am Mohsin from Blogging Bits. I am an aspiring young blogger, and I write about creating successful blogs over at Blogging Bits. Be sure to drop by my place for a drink!










15 Comments
This is great. What a wonderful way to feature a specific post. Have you found a way to randomize this? The Plugin I was using to feature random past posts is no longer supported and this would be brilliant to randomize to stir up posts from your blog archives and feature them.
Thank you!
wow! really it’s working for me….
Thanks
Yep. I side with Lorelle. I want the same thing, randomized and category-based, so I can specify something like ‘display here any of the posts with e.g. both the “pencast” and “featurable” categories’.
I am also looking for a way to randomize this, and I am sure this can be done with some simple PHP loop thingy. But I wouldn’t pretend to be a PHP guru, so I don’t promise anything.
If I figure out a solution, I’ll let you guys know.
Good one. I did not know about the get_a_post plugin, I had written my own function on the same lines.
Incidentally, in one of the projects we used it for a very different reason. The homepage was supposed to be a combination of static text and listing of recent posts. Instead of putting the text directly in the homepage template, we let the author enter the static text in a private WordPress post, and we retrieved the post where the static text was to be displayed. This allowed the author to use the WYSIWYG editor to edit the static content, and we were happy since he would not have to edit the template files
We can also use this to centralize a piece of content that repeats across a site into a WordPress private post, and retrieve it wherever we want it.
I had been using it. I re designed my home page recently and this came very hand
I do it a little differently (see http://blogs.ntugo.com/computers). I have a “Featured” category, then I use the Query_Posts() function. It works great, and it gives you a lot of control.
Those looking for random post functionality, I’ve put up something that provides it:
http://wordpress.pastebin.ca/742771
A benefit here is the code provides a post loop of sorts, so there’s no need to place the code in your template multiple times (that is, for each post).
I wrote something to do this without any plugin (because get-a-post made troubles on my admin panel)
Seems to be okay.
If its just 1 sticky post then why even install a plugin, just use simple html to add a link to the sidebar or wherever needed and it would be all set to go.
@ Prashant:
Simple HTML means editing a Theme template file, a foreign country to millions of WordPress users. When the Theme is upgraded, the HTML is gone and must be replaced. Using a Plugin bypasses those issues.
The plugin looks good, Is there any way to do it manually with hand-coding.
thanks, i was trying to solve this problem on my website but found the solution here. Thanks again.
This is a great plugin, but i have a question, i want to show a post on my startpage. In which file should i write the code so that it is displayed on the startpage, only on the startpage.
thanks,…
@ Thomas:
You do not need a Plugin or file. Just go to your Settings Panel and set the “home” or “front” page to be whatever post or Page you would like it to be.
3 Trackbacks/Pingbacks
[...] WordPress Tip: How to Display a Specific Post Anywhere You Want: Want to feature a post on your blog anywhere in your WordPress Theme? This tip by Mohsin at Blogging Bits will show you how to do it easily. [...]
[...] her, she is the Queen of content, the wizard of WordPress one of my personal favorites Lorelle of Lorelle on WordPress. She always has some great information from WordPress News, design, plugins and so much more. If [...]
[...] WordPress Tip: How to Display a Specific Post Anywhere You Want at Lerelle on WordPress. [...]
Post a Comment