Skip to main content

Ivan Teoh

Something personal yet public

WordPress Theme Development 17 - Archives Template

We decided to change the archives template to be different from main index template. Archives means to be summary of a category, tag or date. The major different between these two template is using template tag, the_excerpt(), in archives template, as shown as below:

98-index-before.php (Source)

<?php
/**
 * @package WordPress
 * @subpackage Your_Theme
 */
?>
<?php get_header(); ?>
<div id="container">
<div id="content">
<!-- archive -->
<?php if ( have_posts() ) : ?>
    <?php $post = $posts[0]; // Hack. Set $post so that the_date() works. ?>
    <?php /* If this is a category archive */ if (is_category()) { ?>
    <h2><?php _e('Archive for the &#8216;') ?><?php single_cat_title(); ?>
      <?php _e('&#8217; Category') ?></h2>
    <?php /* If this is a tag archive */ } elseif( is_tag() ) { ?>
    <h2><?php _e('Posts Tagged &#8216;') ?><?php single_tag_title(); ?>
      <?php _e('&#8217;') ?></h2>
    <?php /* If this is a daily archive */ } elseif (is_day()) { ?>
    <h2><?php _e('Archive for') ?> <?php the_time('F jS, Y'); ?></h2>
    <?php /* If this is a monthly archive */ } elseif (is_month()) { ?>
    <h2><?php _e('Archive for') ?> <?php the_time('F, Y'); ?></h2>
    <?php /* If this is a yearly archive */ } elseif (is_year()) { ?>
    <h2><?php _e('Archive for') ?> <?php the_time('Y'); ?></h2>
    <?php /* If this is an author archive */ } elseif (is_author()) { ?>
    <h2><?php _e('Author Archive') ?></h2>
    <?php /* If this is a paged archive */ } elseif (isset($_GET['paged']) &&
      !empty($_GET['paged'])) { ?>
    <h2><?php _e('Blog Archives') ?></h2>
    <?php } ?>
    <?php while (have_posts()) : ?>
        <?php the_post(); ?>
        <div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
        <div class="entry-title">
        <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php
          printf( __('Permalink Link to %s'), the_title_attribute('echo=0') );
          ?>"><?php the_title(); ?></a></h2>
        </div><!-- .entry-title -->
        <div class="entry-content">
        <?php the_excerpt(); ?>
        <p><?php wp_link_pages(); ?></p>
        </div><!-- .entry-content -->
        <div class="entry-meta">
        <p><?php _e('Posted by'); ?> <?php the_author(); ?> @ <?php
          the_time('F jS, Y'); ?> <?php _e('in'); ?> <?php the_category(',');
          ?> <?php the_tags(__('with '), ', '); ?></p>
        <p><?php comments_popup_link(__('No Comments &#187;'),
          __('1 Comment &#187;'), __('% Comments &#187;')); ?> <?php
          edit_post_link(__('Edit This'), '&#8212;'); ?></p>
        </div><!-- .entry-meta -->
        </div>
        <?php comments_template(); ?>
    <?php endwhile; ?>
    <p><?php next_posts_link(__('&laquo; Older Posts')); ?></p>
    <p><?php previous_posts_link(__('Newer Posts &raquo;')); ?></p>
<?php else : ?>
<h2><?php _e('Not Found'); ?></h2>
<p><?php _e('Sorry, but you are looking for something that isn&#39;t here.');
  ?></p>
<?php endif; // END: if ( have_posts() ) ?>
</div><!-- #content -->
</div><!-- #container -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>

But the default, using this template tag, the_excerpt(), will be showing first 55 words of the post's content. I do felt it is a bit long for me. I need to customized it. But how? Arron showed us how to customize this template tag, the_excerpt(). We need to add customized excerpt function to replace the default excerpt function, wp_trim_excerpt in theme functions file, functions.php as shown at below. Currently the only changes that different from default is at line 57. Changed the "55" to "8".

98-index-after.php (Source)

<?php
/**
 * Customized function for wp_trim_excerpt
 */
function customized_trim_excerpt($text) {
    $raw_excerpt = $text;
    if ( '' == $text ) {
        $text = get_the_content('');

        $text = strip_shortcodes( $text );

        $text = apply_filters('the_content', $text);
        $text = str_replace(']]>', ']]&gt;', $text);
        $text = strip_tags($text);
        $excerpt_length = apply_filters('excerpt_length', 8);
        $words = explode(' ', $text, $excerpt_length + 1);
        if (count($words) > $excerpt_length) {
            array_pop($words);
            array_push($words, '[...]');
            $text = implode(' ', $words);
        }
    }
    return apply_filters('wp_trim_excerpt', $text, $raw_excerpt);
}

remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'customized_trim_excerpt');
?>

Comments

Comments powered by Disqus