wordpress post_excerpt length limit 15

I wanted limit my post_excerpt length 15 and here is :

    function expert_shortcode($atts){
    extract( shortcode_atts( array('title' => '','category' => ''), $atts, 'expert' ) );

    $q = new WP_Query(
        array('category_name' => $category, 'posts_per_page' => '5', 'post_type' => 'post')
        );

        $list = '<div class="left_top"><h3 class="left_top_title"> '.$title.'</h3>';
    while($q->have_posts()) : $q->the_post();
    //get the ID of your post in the loop
    $id = get_the_ID();
    $post_excerpt = get_post_meta($id, 'post_excerpt', true);  
    $post_thumbnail=get_the_post_thumbnail( $post->ID, 'thumbnail');    
    $list .= '

                <div class="left_top ">
                    '.$post_thumbnail.'
                    <h3><a href="'.get_permalink().'">'.get_the_title().'</a></h3>
                    <p>'.$post_excerpt.'</p>
                </div>

    ';        
endwhile;
$list.= '</div>';
wp_reset_query();
return $list;
}

add_shortcode('expert', 'expert_shortcode');    

Its working with custom field input, But I wanted limit word length 15.

Related posts

Leave a Reply

2 comments

  1. Since, you are using a custom field we will have to write a function of our own. Anyways, PHP is pretty cool and gives us explode. Here’s how we will do it:

    function custom_excerpt( $excerpt ) {
        $parts = explode( $excerpt, ' ', 15 ); // explode string after 15 spaces
        return $parts[0]; // taking the first part of the array
    }
    

    If you were using the_excerpt, you could have written a filter to control length:

    function custom_excerpt_length( $length ) {
        return 20;
    }
    add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
    
  2. you can try below code

    substr($post_excerpt, 0, 15);
    

    limit with words

     $words = explode(' ', $post_excerpt);
     echo implode(' ', array_slice($words, 0, 15));
    

    your code would be like

    add_filter('pre_get_posts', 'set_post_order_in_admin' );
    
     function expert_shortcode($atts){
    extract( shortcode_atts( array('title' => '','category' => ''), $atts, 'expert' ) );
    
    $q = new WP_Query(
        array('category_name' => $category, 'posts_per_page' => '5', 'post_type' => 'post')
        );
    
        $list = '<div class="left_top"><h3 class="left_top_title"> '.$title.'</h3>';
    while($q->have_posts()) : $q->the_post();
    //get the ID of your post in the loop
    $id = get_the_ID();
    $post_excerpt = get_post_meta($id, 'post_excerpt', true);  
     $words = explode(' ', $post_excerpt);
    $data=implode(' ', array_slice($words, 0, 15));
    $post_thumbnail=get_the_post_thumbnail( $post->ID, 'thumbnail');    
    $list .= '
    
                <div class="left_top ">
                    '.$post_thumbnail.'
                    <h3><a href="'.get_permalink().'">'.get_the_title().'</a></h3>
                    <p>'.$data.'</p>
                </div>
    
    ';        
    endwhile;
     $list.= '</div>';
    wp_reset_query();
      return $list;
    }
    
     add_shortcode('', 'expert_shortcode');