- WordPress 2.7 Cookbook
- Jean-Baptiste Jung
- 432字
- 2021-04-01 13:55:52
Making your new posts stands out with a custom style
Creating a Featured Posts block, as we have seen earlier, allows you to specify some posts to be displayed on your blog as the featured posts. However, you may prefer to automatically make your latest posts stand out. In this recipe, we shall learn to modify the classic WordPress loop to give another style to your latest posts.

Getting ready
Similar to the previous recipe, you only need a text editor, a WordPress theme, and—of course—this book to achieve the hack. This recipe is based on the WordPress default theme, but can be adapted on any other theme as well.
How to do it...
- Open the
index.php
file from your theme and look for the WordPress loop. In the WordPress default theme, the WordPress loop is located on line 5 and looks similar to the following code:<?php while (have_posts()) : the_post(); ?> <div class="post" id="post-<?php the_ID(); ?>"> <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2> <small><?php the_time('F jS, Y') ?><!-- by <?php the_author() ?> --></small> <div class="entry"> <?php the_content('Read the rest of this entry »'); ?> </div> <p class="postmetadata"><?php the_tags('Tags: ', ', ', '<br />'); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?><?php comments_popup_link('No Comments ?', '1 Comment ?', '% Comments ?'); ?></p> </div> <?php endwhile; ?>
- Replace that loop with the following code and save the file. You can edit the number of posts that will be marked as
"featured"
on line 4. As the earlier code displays only one featured post, let us use the following code.<?php $count = 0; ?> <?php while (have_posts()) : the_post(); ?> <?php $count++; ?> <?php if ($count < 2) : ?> <div class="featured post" id="post-<?php the_ID(); ?>"> <h2><a href="<?php the_permalink() ?>">New Post: <?php the_title(); ?></a></h2> <small>Posted in <?php the_category(', ') ?> on <?php the_time('F jS, Y') ?>.</small> <div class="entry"> <?php the_content('Read the rest of this entry »'); ?> </div> </div> <?php else : ?> <!-- "Normal" posts --> <div class="post" id="post-<?php the_ID(); ?>"> <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2> <small><?php the_time('F jS, Y') ?></small> <div class="entry"> <?php the_content('Read the rest of this entry »'); ?> </div> <p class="postmetadata"><?php the_tags('Tags: ', ', ', '<br />'); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?><?php comments_popup_link('No Comments ?', '1 Comment ?', '% Comments ?'); ?></p> </div> <?php endif; ?> <?php endwhile; ?>
- Now, edit the
style.css
file from your theme. The preceding code creates and adds a"featured"
class to your latest posts—with the help of the"featured"
class you can modify the post displays. Add the following code to thestyle.css
file and style it your way:post.featured { background:#f9f9f9; font-weight:bold; }
How it works...
At the beginning of the code, and before the loop, I have created a PHP variable named $count
and initialized at the value 0
. This variable is a counter—on each iteration through the loop, the code changes its value to its current value + 1.
On line 4 of the earlier code, I test whether the $count
value is smaller than 2
. If yes, a post with the "featured"
style is displayed, otherwise the posts are displayed in normal style.
There's more...
The PHP counters are very useful and can be used to achieve various tasks. For example, the following code, similar to our previous code will display some AdSense ads after the first post:
<?php $count = 0; ?> <?php while (have_posts()) : the_post(); ?> <?php $count++; ?> <div class="post" id="post-<?php the_ID(); ?>"> <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2> <small><?php the_time('F jS, Y') ?></small> <div class="entry"> <?php the_content('Read the rest of this entry »'); ?> </div> <p class="postmetadata"><?php the_tags('Tags: ', ', ', '<br />'); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?><?php comments_popup_link('No Comments ?', '1 Comment ?', '% Comments ?'); ?></p> <?php if ($count < 2) : ?> <!-- INSERT ADSENSE CODE HERE --> <?php endif; ?> </div> <?php endwhile; ?>
That's it! Your latest posts are now displayed with a custom style.
In this chapter, you have learned how to modify and enhance a WordPress theme to definitely make your blog different.
- Photoshop+Camera Raw風(fēng)光、人文、城市、星空攝影后期技法(全彩)
- Moodle 2.0 E/Learning Course Development
- 性能測(cè)試從零開(kāi)始
- AI繪畫(huà)教程:Midjourney使用方法與技巧從入門(mén)到精通
- 四時(shí)風(fēng)月繪:國(guó)風(fēng)水墨CG插畫(huà)繪畫(huà)技法
- Photoshop CS6 互聯(lián)網(wǎng)應(yīng)用設(shè)計(jì)教程
- Photoshop CS6從入門(mén)到精通
- Maya 2019三維動(dòng)畫(huà)基礎(chǔ)案例教程
- Joomla! 1.5 Site Blueprints: LITE
- Capture One 22 Pro高級(jí)實(shí)戰(zhàn)教程
- Adobe創(chuàng)意大學(xué)Photoshop CS5 產(chǎn)品專(zhuān)家認(rèn)證標(biāo)準(zhǔn)教材
- 中文版Photoshop CC平面設(shè)計(jì)實(shí)用教程
- Photoshop CC完全自學(xué)教程:從入門(mén)到實(shí)踐(全新版)
- Magento: Beginner's Guide
- LaTeX入門(mén)與實(shí)戰(zhàn)應(yīng)用