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.
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:If you were using
the_excerpt
, you could have written a filter to control length:you can try below code
limit with words
your code would be like