使用mb_strimwidth函数时不读取加密文章摘要的方法

09-28 | 夜光 | IT记录

使用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; ?>
本文标签:
本文链接: mb-strimwidth-post-password-required/
版权所有: 玻璃泉, 转载请注明本文出处。