使用mb_strimwidth函数可以实现首页index与存档页archive等的文章摘要,但问题是即使文章设置了密码,该函数仍会截取摘要,可以利用wordpress的加密日志函数post_password_required来解决。
原来的首页index.php与存档页archive.php中截取摘要的代码:
<?php echo mb_strimwidth(strip_tags(apply_filters('the_content',$post->post_content)),0,300,"[......]");?> <p></p><p><a rel="nofollow" href="<?php the_permalink();?>"><?php _e('Read More»','YLife');?></a></p>
将其修改为如下代码即可:
<?php if(post_password_required()): ?> <?php the_content(); ?> <?php else : ?> <?php echo mb_strimwidth(strip_tags(apply_filters('the_content',$post->post_content)),0,300,"[......]");?> <p></p><p><a rel="nofollow" href="<?php the_permalink();?>"><?php _e('Read More»','YLife');?></a></p> <?php endif; ?>