Fix for the the_content_limit() function (limit posts plugin) when previewing a single post

Learn more here!

The limit-posts plugin, or the_content_limit() function as it is widely known, is a plugin/function which shortens your posts, usable when wanting to display post excerpts on the home or index page.

It is used by many premium (example: revolution from studio press) themes as a built in function.

However, it can create problems if you are trying to show an excerpt of a post in the sidebar, when you are previewing a post in a single post view.

I was struggling with it, but finally coded out a perfect solution. You can use the following code:

Instead of posting

<?php the_content_limit(100,"Read more &rarr;");?>

Use

<?php
if (is_single()) {
echo substr(get_the_content(),0,150)."... <a href=".get_permalink().">Read More &rarr;</a>";
} else {
the_content_limit(150,"Read More &rarr;");
}
?>

This function will check if you are previewing a post in a single view mode, if so, it will create a custom excerpt similar, if not the same as php the_content_limit(100, “more”).

One Response to “Fix for the the_content_limit() function (limit posts plugin) when previewing a single post”
  1. great workaround! thanx a lot!

Leave a Reply