在贴吧、wordpress群上经常看到有人修改wordpress的默认主题,于是,我也心血来潮,修改了一下下,为了避免将来整理修改过程麻烦,或者遗漏了什么问题,现在开始把我修改的每一步,能记录的都记录下来。
文章内容截断,很简单的代码就能搞定,动手之前,还是建议你看完全文了再去修改。
本文撰写时间较早,内容可能已经失效,仅作记录、参考!
在默认主题twentytwelve的后台或根文件夹下找到function.php,用代码编辑器打开,在最后一个?>
前插入下面这段代码:
function my_excerpt_length($length) { return 180; } add_filter('excerpt_length', 'my_excerpt_length');
|
然后,在你需要实现文章截断的文件里修改,将类似于<?php the_content(); ?>
替换为<?php the_excerpt();?>
其实就是在你调用文章的页面里,把调用文章的全文改为调用文章的摘要。不懂的可以自己去度娘,我也是新手,说得不够专业。
我的方法是,重建一个content-jied.php文件,加入下面的代码
<?php
?> <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> <?php if ( is_sticky() && is_home() && ! is_paged() ) : ?> <!--<div class="featured-post"> <?php _e( 'Featured post', 'twentytwelve' ); ?> </div>--> <?php endif; ?> <header class="entry-header"> <?php the_post_thumbnail(); ?> <?php if ( is_single() ) : ?> <h1 class="entry-title"><?php the_title(); ?></h1> <?php else : ?> <h1 class="entry-title"> <a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( sprintf( __( 'Permalink to %s', 'twentytwelve' ), the_title_attribute( 'echo=0' ) ) ); ?>" rel="bookmark"><?php the_title(); ?></a> </h1> <?php endif; // is_single() ?> <?php if ( comments_open() ) : ?> <div class="comments-link"> <?php comments_popup_link( '<span class="leave-reply">' . __( 'Leave a reply', 'twentytwelve' ) . '</span>', __( '1 Reply', 'twentytwelve' ), __( '% Replies', 'twentytwelve' ) ); ?> </div><!-- .comments-link --> <?php endif; // comments_open() ?> </header><!-- .entry-header --> <?php if ( is_search() ) : // Only display Excerpts for Search ?> <div class="entry-summary"> <?php the_excerpt(); ?> </div><!-- .entry-summary --> <?php else : ?> <div class="entry-content"> <?php the_excerpt( __( 'Continue reading <span class="meta-nav">→</span>', 'twentytwelve' ) ); ?> <?php wp_link_pages( array( 'before' => '<div class="page-links">' . __( 'Pages:', 'twentytwelve' ), 'after' => '</div>' ) ); ?> </div><!-- .entry-content --> <?php endif; ?> <footer class="entry-meta"> <?php twentytwelve_entry_meta(); ?> <?php edit_post_link( __( 'Edit', 'twentytwelve' ), '<span class="edit-link">', '</span>' ); ?> <?php if ( is_singular() && get_the_author_meta( 'description' ) && is_multi_author() ) : // If a user has filled out their description and this is a multi-author blog, show a bio on their entries. ?> <div class="author-info"> <div class="author-avatar"> <?php echo get_avatar( get_the_author_meta( 'user_email' ), apply_filters( 'twentytwelve_author_bio_avatar_size', 68 ) ); ?> </div><!-- .author-avatar --> <div class="author-description"> <h2><?php printf( __( 'About %s', 'twentytwelve' ), get_the_author() ); ?></h2> <p><?php the_author_meta( 'description' ); ?></p> <div class="author-link"> <a href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ); ?>" rel="author"> <?php printf( __( 'View all posts by %s <span class="meta-nav">→</span>', 'twentytwelve' ), get_the_author() ); ?> </a> </div><!-- .author-link --> </div><!-- .author-description --> </div><!-- .author-info --> <?php endif; ?> </footer><!-- .entry-meta --> </article><!-- #post -->
|
然后保存在主题根目录下,在index.php调用content的地方修改为:
< ?php while ( have_posts() ) : the_post(); ?> < ?php get_template_part( 'content-jied', get_post_format() ); ?> < ?php endwhile; ?>
|
不建议直接复制,最好是搞懂了,自己新建一个文件去修改,因为这款主题比较复杂,而且我也修改了N遍,不保证我会漏掉哪一步没有说,并且我几乎修改了所有的PHP文件,不清楚是否会有关联。
自己动手试试吧,保存并更新你的主题,点击查看下,是不是出现了截断啊!