PHP snippet for WordPress
So I decided to build my own WordPress theme from scratch. I know, it seems like that is all I’m posting about, but that is what I am wanting to learn right now…and I try to post what I am learning. Here is one of the most recent things I have done.
Most WordPress sites out there have 10 posts on the home page, and it is the whole post. It can make for a very long page. I prefer to have a lot of the most recent page on the home page, and then excerpts of the other recent ones. The function “the_excerpt()” does not let you specify the amount of characters, so I either had the whole post, or just a little.
On this site I limit the amount of characters to 6,000 by using a function (not in WP by default, if you need it, let me know via email) called “the_content_limit()”. I thought that was a great thing, but it stripped all of the html. I wanted to keep my breaks, so I wrote this:
$content = strip_tags($content, '<p><br><br />');
$content6 = substr($content, -6); // grab last 6 chars
$last6array = str_split($content6); // split the 6 in an array
if (in_array("<", $last6array)) {
$content = substr($content, 0, -6);
}
The code just checks to see if the last 6 characters have a < and if so, strip it. We want to make sure that all tags are closed.