如何将摘录长度从55减少到我想要的值

时间:2016-05-25 11:10:26

标签: php wordpress

     <?php

     $paged = ( get_query_var('page') ) ? get_query_var('page') : 2;

   $query_args1 = array(
   'post_type' => 'post',
    'category_name' => 'blog',
    'posts_per_page' => 4,
     'offset'=>6,
     'paged' => $paged
     );

     $the_query = new WP_Query( $query_args1 );
     ?>
    <?php
     while ( $the_query->have_posts() ) : $the_query->the_post();
    ?>
   <?php 
    if (has_post_thumbnail()){
    $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );   ?>
    <div class="hentry-post group">
    <div class="thumb-img"><img src="<?php echo $url;?>" width="65px">               </div>
     <div class="text">
     <h5><?php the_title_attribute(); ?></h5>
     <p><?php the_excerpt('20'); ?> </p>
      <!--<a class="read-more pull-right" href="#">→ Read More</a> </div>-->
  </div>
   </div>

我的函数.php代码减少长度是.....

  function custom_excerpt_length( $length ) {
   return 20;
   }
   add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );

任何人都可以告诉我如何处理内容,尽可能显示最少的内容.....

1 个答案:

答案 0 :(得分:0)

在functions.php .......................................中添加了以下代码。

      function excerpt($limit) {
      $excerpt = explode(' ', get_the_excerpt(), $limit); 
       if (count($excerpt)>=$limit) { 
      array_pop($excerpt);
      $excerpt = implode(" ",$excerpt).'...';
       } else { 
      $excerpt = implode(" ",$excerpt); }   $excerpt = preg_replace('\[[^\]]*\]','',$excerpt); 
        return $excerpt; 
       } 
       and wherever to use just add 
       <?php echo $excerpt(What_length_you_want);?>