- WordPress 2.7 Cookbook
- Jean-Baptiste Jung
- 343字
- 2021-04-01 13:55:51
Creating a Featured Posts block on your homepage
Would you like to make some of your posts stands out of the crowd? If yes, a good solution is to create a Featured Posts block on your blog homepage.

Getting ready
As you can see in the preceding screenshot, before the normal post listing, the blogger set up a Featured Posts block where he or she display the most popular posts.
To achieve this recipe, you only need a text editor and a WordPress theme.
How to do it...
- Log in to your WordPress Dashboard, go to Manage | Categories, and create a new category named Featured.
- Once you have created your Featured category, simply put your mouse cursor on the edit link related to this category and look at your browser's status bar—you'll be able to see the
action=edit
and category ID (cat_ID=1
, in our case), as shown in the following screenshot: - Open the
index.php
file from your theme directory. - At the location where you want your Featured Posts to appear, insert the following code:
<?php if (!$paged) { ?> <div id="featured"> <?php query_posts('showposts=5&cat=5); ?> <h2>Featured posts</h2> <?php while (have_posts()) : the_post(); ?> <div class="featuredElement"> <h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3> <?php the_excerpt(); ?> </div><!--/featuredElement--> <?php endwhile; ?> </div><!--/featured-->
- Don't forget to modify the category parameter on line 4 of the preceding code according to your
"featured"
category ID. - Save the file, upload it on the
wp_content/themes/yourtheme
directory of your server, and go back to your WordPress dashboard.
To create featured posts, simply add it to the "featured"
category. The 5 most recent posts will be shown on your blog homepage.
How it works...
The earlier stated code starts by looking for the $paged
variable. If the page isn't paginated, which means the reader is looking at the blog homepage only, the code executes the query_posts()
function with the showposts
parameter—to display only 5
posts and the cat
parameter. This allows us to only get posts from a certain category, that is, the featured category we had created before.
There's more...
Another interesting use of the Featured Posts block in your blog homepage is to create a widget-ready zone. Widgets will be discussed in the later chapters of this book.
- CAXA CAD電子圖板2020工程制圖
- VMware虛擬化與云計算:vSphere運維卷
- AutoCAD Civil 3D 2018 場地設計實例教程
- 從零開始:Photoshop工具詳解與實戰(zhàn)
- Java EE 6 with GlassFish 3 Application Server
- 網(wǎng)店美工設計:Photoshop CC案例教程(視頻指導版)
- Backbone.js Cookbook
- WCF Multi/tier Services Development with LINQ
- Adobe創(chuàng)意大學InDesign產(chǎn)品專家認證標準教材(CS6修訂版)
- 魔法詞典:AI繪畫關鍵詞圖鑒(Midjourney版)
- 機械CAD軟件應用入門指導書
- Flash CS5動畫設計教程
- SolidWorks 2020中文版入門、精通與實戰(zhàn)
- 中文版Photoshop CS6經(jīng)典自學教程(培訓教材版)
- Oracle Modernization Solutions