wordpress調用文章摘要,若無摘要則自動截取文章內容字數做為摘要
- 2019 年 10 月 7 日
- 筆記
以下是調用指定分類文章列表的一個方法,作者如果有填寫文章摘要則直接調用摘要;如果文章摘要忘記寫了則自動截取文章內容字數做為摘要。這個方法也適用於調用description標籤
<ul> <?php $args=array( 'cat' => 1, // 分類ID 'posts_per_page' => 10, // 顯示篇數 ); query_posts($args); if(have_posts()) : while (have_posts()) : the_post(); ?> <li> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> //標題 <p> <?php if (has_excerpt()) { echo $description = get_the_excerpt(); //文章編輯中的摘要 }else { echo mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 170,"……"); //文章編輯中若無摘要,自動截取文章內容字數做為摘要,0表示開始的位置,170表示結束的位置 } ?> </p> </li> <?php endwhile; endif; wp_reset_query(); ?> </ul>
這個方法是不是還不錯,感興趣的朋友可以去試試。