WordPress 首頁(主頁)輸出文章摘要都是使用 <?php the_content(); ?> 函數,但在某些主題風格中,主頁一般不是完全輸出文章內容的——而是輸出摘要或者截斷字數輸出限制;比如一些主題作者常常會用 <?php the_content(); ?> 代碼,如果換作 mb_strimwidth 自動截字函數,若是文章加上密碼保護,就會在主頁暴露文章的內容了。
如果你的WP主題是使用 mb_strimwidth 函數,用作限制字符串長度的,那麼有些文章加上密碼保護功能,主頁就會顯示部份內容,那麼!請適當修改代碼,以修正這個Bugs...
一般自動截斷內容字數,會用到以下的代碼:
<?php echo mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0,80,"..."); ?>
以上代碼!會在首頁暴露受密碼保護的內容。
我們需要加上一個 post_password_required 判斷密碼函數,判斷內容是否為密碼保護文章,在首頁要求訪客輸入密碼才可以看到內容。
<?php if (post_password_required()):the_content(); else : ?><?php echo mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 80,"...");endif; ?>
或是
<?php if(post_password_required()): ?>
<?php the_content(); ?>
<?php else : ?>
<?php echo mb_strimwidth(strip_tags(apply_filters('the_content',$post->post_content)),0,80,"..."); ?>
<p><a rel="nofollow" href="<?php the_permalink();?>">Read More»</a></p>
<?php endif; ?>
自定義密碼保護文章摘要的提示信息:
<?php if (post_password_required()): echo '文章密碼保護!'; else : ?><?php echo mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 80,"...");endif; ?>