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!













31 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.
This is great, just what i needed for my clients website. Big Thank you!
I have a Page on my site and want to display the most recent post in a category, even though it’s a Page.
Will this plugin work for me, and how?
Thank you.
@ michael:
There is a lot of complexity in putting categories on Pages, since they do not act the same as posts. I’ve not heard of a Plugin that can pull Pages into most recent posts, but there may be one. Good luck.
Awesome! Exactly what i needed. I have noticed however it seems to remove formatting and hyperlinks….. it just ignores any , or ’s and displays it as a stream of text- any ideas?
I figured it out
i just used a php “the_content” insert…. obviously the content_extract only extracts text, without formatting….
thanks for heading me on the right path, all great now!
@ Nils:
Ah, now I understand what you are looking for. Just for clarification, it’s
the_excerptthat strips all the code for displaying excerpts on the front page and multi-post pageviews on WordPress blogs. Changing it tothe_contentputs the WHOLE post on the multi-post pageviews, which you may not want, though you can control the length by using the “more” button/tag.The excerpt template tag is used for a very good reason on multi-post pageviews. It keeps images and code that can mess up the design on those types of pages from screwing things up. People click through to the whole post to see everything. I recommend that you change it only on the front page but leave
the_excerpttemplate tag in place for other multi-post pageviews like searches, categories, tags, and archives.This is great, but it seems limit only one specific post to be displayed each time. How we can make it multi-specific post to be displayed in different post? Perhaps control by custom field value to assign the value? any solution?
You want to display many posts in a different post? In the sidebar? Do you need a list, like “Related Posts” in the bottom of the post? Mohsin at Blogging Bits is the author of this tip on my blog, so you can ask him directly on his blog if he doesn’t check in here soon, or ask in the WordPress Support Forums. Until I’m clearer on what you want, it’s hard for me to answer.
I mean I want to get a specific post to display on certain post only.
Let say i want to display an old post ‘x1′ at the head of post ‘A’, post ‘B’ and Post ‘C’, but in the meantime i also want to display another old post ‘x2′ in the head of post ‘D’ and Post ‘E’. As you see, if ‘x1′ posted ,then post ‘D’ and ‘E’ are unable to display post ‘x2′ at their head, except I create another template for post ‘D’ and ‘E’…you get what i mean?( sorry of my poor English)
All I need is let the $id in get_a_Post($1d) varies with the new post. So the ‘x1′ will be display in post A,B and C, and post D and E will only display ‘x2′.
Is it possible to make the $id customizable by using the custom field value?
Hope You will understand what i trying to express.
Thanks.
I’m still traveling and don’t have a lot of time to dig into this, so I recommend you ask on the WordPress Support Forums. You can link to any post on any page, directing people there by your link recommendation in many ways, but this sounds like you want to mix and match your post content on pages, which sounds very much like a spam blog technique.
So for now, this is my feeling on this issue. If you want to recommend a past blog post, link to it and tell your readers why. Use a Related Posts WordPress Plugin.
If you would like to showcase a past post in a “featured post” area on your blog post area or in the sidebar or front page, then use a featured post or “from the archives” style WordPress Plugin.
If you want to pull in information, like a custom landing page greeting or something (say someone arrives on a blog post in the Dog category on your blog, they would be greeted with something that says “If you would like more information on Dogs, see these posts”) then that is a landing page WordPress Plugin. Or you could use Conditional Tags with information in the WordPress Codex on how to do that.
I am an advocate of the post content being original, not duplicated. Duplicated content is messy and can get you in trouble. I can’t help you mix and match content on a blog post as that goes against everything I believe in for trust and establishing relationships through blogs. Maybe you’ll find someone in the WordPress Support Forum to help, or maybe I’m not understanding.
Thank you, this is very helpful. I am trying to make this work with the Inline PHP plugin and I keep getting error messages. If you could suggest the right syntax to display a post using Inline PHP, I would appreciate it! Thanks again.
I’m not familiar with that Plugin. Talk to the Plugin author for help and support. Thanks.
HI
can you GET specific author posts, instead of a specific post?
This is a nice plugin, does it work with the latest WordPress 2.8? Anyway, I will download and give it a try… good for customizing my theme.
Did you try it? It should work. There are also newer methods of doing this, some of which are listed in the comments.
My apologies – I’m new to WordPress and I have no idea what to do. Any way I could ask a few questions?
1 – I want to post an article on a specific page “Research”. You say to post this in the “template file where I want the post to appear”(my post ID is 47:
<a href="”>
Where do I find the template file? How do I figure out in the template file where the Research Page is?
I tried putting your code directly on the Research Page, but that didn’t work…help!
Thanks so much -
4 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. [...]
[...] Forum verbinden, und dann auch noch was nettes hier z.B umd z.b die Last Post anzeigen zu lassen. WordPress Tip: How to Display a Specific Post Anywhere You Want Lorelle on WordPress __________________ [...]