官术网_书友最值得收藏!

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.

Creating a Featured Posts block on your 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...

  1. Log in to your WordPress Dashboard, go to Manage | Categories, and create a new category named Featured.
  2. 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:
    How to do it...
  3. Open the index.php file from your theme directory.
  4. 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-->
  5. Don't forget to modify the category parameter on line 4 of the preceding code according to your "featured" category ID.
  6. 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.

主站蜘蛛池模板: 苗栗市| 浦江县| 太康县| 呼图壁县| 岑溪市| 昌都县| 乐亭县| 泗阳县| 房产| 搜索| 攀枝花市| 黄龙县| 阜康市| 邵阳市| 博爱县| 苏尼特左旗| 福鼎市| 太原市| 甘肃省| 台山市| 丰镇市| 申扎县| 滕州市| 民丰县| 桦南县| 淮安市| 新巴尔虎右旗| 怀来县| 万山特区| 五常市| 海丰县| 崇信县| 武强县| 莱阳市| 衡东县| 巴中市| 邹平县| 乌鲁木齐县| 霍林郭勒市| 龙江县| 九江县|