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

  • 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.

Making your new posts stands out with a custom style

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...

  1. 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 &raquo;'); ?>
        </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; ?>
  2. 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 &raquo;'); ?>
          </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 &raquo;'); ?>
          </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; ?>
  3. 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 the style.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 &raquo;'); ?>
    </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.

主站蜘蛛池模板: 中牟县| 永清县| 巴塘县| 泊头市| 获嘉县| 凤阳县| 高安市| 桃园市| 石屏县| 汉川市| 保山市| 岳阳县| 海盐县| 墨玉县| 潢川县| 读书| 夹江县| 酉阳| 休宁县| 南召县| 万州区| 康保县| 建始县| 民乐县| 巨鹿县| 呈贡县| 盐池县| 肃宁县| 卢龙县| 大连市| 濮阳市| 漠河县| 嘉荫县| 新民市| 大洼县| 齐河县| 荔浦县| 舞钢市| 朔州市| 比如县| 曲靖市|