Limit length of post characters in WordPress with different ways







There are different  way to limit the posts characters in wordpress.

1). First method is that we can use any of these plugins to limit post content.
http://wordpress.org/plugins/limit-post-add-on
https://wordpress.org/plugins/auto-limit-posts-reloaded/

—————————————————————————

2). Second method is that we can create a function and add action with excerpt_length.

function new_excerpt_length($length) {
return 30;
}
add_filter('excerpt_length', 'new_excerpt_length');

————————————————————————-

3). Third method is that we can use php function

echo  substr(strip_tags($post->post_content), 0, 546);

—————————————————————————-

4).Fourth method is that we can use wp_query class of wordpress.

$cwwp_query = new WP_Query(
array(
'post_type' => 'post',
'posts_per_page' => 1
)
);

if ( $cwwp_query->have_posts() ) :
while ( $cwwp_query->have_posts() ) :
$cwwp_query->the_post();
echo '<h2>' . get_the_title() . '</h2>';
echo '<p>' . wp_trim_words( get_the_content(), 80 ) . '</p>';
endwhile;
endif;

wp_reset_postdata();

—————————————————————————-

5).There is another way to limit the characters by using these functions.

function limit_content($limit) {
$limit_content = explode(' ', get_the_excerpt(), $limit);
if (count($limit_content)>=$limit) {
array_pop($limit_content);
$limit_content = implode(" ",$limit_content).'...';
} else {
$limit_content = implode(" ",$limit_content);
}
$limit_content = preg_replace('`[[^]]*]`','',$limit_content);
return $limit_content;
}

To call the function we can add this

where 25 is the limit you can add any number.

—————————————————————————

6. Use wp_trim_words() function of wordpress

echo wp_trim_words( get_the_content(), 40, '...' );

Thanks
i hope these functions will you…

Share With Friends

Also Read   Display latest post in wordpress with Time ago format

Speak Your Mind

*

Time limit is exhausted. Please reload CAPTCHA.