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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
| <?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 ‘') ?><?php single_cat_title(); ?><?php _e('’ Category') ?></h2>
<?php /* If this is a tag archive */ } elseif( is_tag() ) { ?>
<h2><?php _e('Posts Tagged ‘') ?><?php single_tag_title(); ?><?php _e('’') ?></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 »'), __('1 Comment »'), __('% Comments »')); ?> <?php edit_post_link(__('Edit This'), '—'); ?></p>
</div><!-- .entry-meta -->
</div>
<?php comments_template(); ?>
<?php endwhile; ?>
<p><?php next_posts_link(__('« Older Posts')); ?></p>
<p><?php previous_posts_link(__('Newer Posts »')); ?></p>
<?php else : ?>
<h2><?php _e('Not Found'); ?></h2>
<p><?php _e('Sorry, but you are looking for something that isn'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".
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
| /** * 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(']]>', ']]>', $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');
|
References
No Comments
No comments yet.