WordPress自动摘要的方法

修改 functions.php 文件

add_filter('the_content', 'autoExcerpts');
function autoExcerpts($content = false) {
  // If is the home page, an archive, or search results
  if(is_front_page() || is_archive() || is_search()) :
    global $post;
    $content = $post->post_excerpt;
    
    // If an excerpt is set in the Optional Excerpt box
    if($content) :
      $content = apply_filters('the_excerpt', $content);
    
    // If no excerpt is set
    else :
      $content = $post->post_content;
      $excerpt_length = 55;
      $words = explode(' ', $content, $excerpt_length + 1);
      if(count($words) > $excerpt_length) :
        array_pop($words);
        array_push($words, '...');
        $content = implode(' ', $words);
      endif;
      $content = '<p>' . $content . '</p>';
    endif;
  endif;
    
  // Make sure to return the content
  return $content;
}

当设置摘要的时候,会显示摘要。没有的时候就自动提取摘要

「点点赞赏,手留余香」

    还没有人赞赏,快来当第一个赞赏的人吧!