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:
There’re three filters that control the »more« link, depending on what function/Template Tag is in use. The bad thing is, that they’re intercepting each other. The good thing is, that you can simply modify the output of the filter using
current_filter()
to retrieve the name of the currently attached filter and modify the output.Then we got the
'excerpt_length'
-filter to limit the excerpt length. This one doesn’t allow us to add a permalink, but it helps us in combination with the other filters. See the 2nd plugin.The permalink-more plugin
This plugin adds the permalink to the content or excerpt – depending on what is displayed. It also resets the
excerpt_more
-filter to output nothing, so it’s not interfering with the other filters.The excerpt-more length plugin
If you just want to modify the lenght of the excerpt, you can use a much simpler filter setup. The following plugin does a very nifty job. It reduces the content (we’re in the loop and have post data to access) to 300 words. In the next step it counts the letters of each single word. Then it simply returns this (dynamically assigned) number.
Notes
the_content()
orthe_excerpt()
in your theme to make use of this plugins.