As you can see into the code, the routine is to display the post which has an id
of 266
. Now all I want is to limit the words displayed in the post content of that post. Let’s say, I want to limit the words to a number of 300 and then add a read more link.
This is the code I got so far:
$post_id = 266;
echo "<div id='widgets-wrapper3'><div id='marginwidgets' style="overflow: auto; max-width: 100%; margin: 0 auto; border: none !important;">";
$queried_post = get_post($post_id);
echo "<div class="thewidgets">";
echo $queried_post->post_content;
echo '</div>';
echo "</div></div>";
?>
I always have the same problem with post excerpt, post content. There’re various hooks and functions for this purpose, like @kaiser pointed out. But sometimes they don’t do exactly what I want.
Here’s my solution, I write my own function that take the post content, and truncate it into specified number of words:
function wpse69204_excerpt( $num_words = 20, $ending = '...', $post_id = null )
{
global $post;
// Truncate post content
$current_post = $post_id ? get_post( $post_id ) : $post;
$excerpt = strip_shortcodes( $current_post->post_content );
$excerpt = wp_trim_words( $excerpt, $num_words, $ending );
// Read more link
$excerpt .= '<a href="' . get_permalink( $post ) . '" title="">Continue reading...</a>';
return $excerpt;
}