In the previous post of the Genesis Explained series I touched on a few functions after explaining very quickly what was in each of the files in the functions folder. I’m going to come back to the admin.php file and skip deprecated.php and feed.php. You really shouldn’t use any functions in deprecated.php and there isn’t much use to the feed.php outside the way the functions are being used.
This brings us to formatting.php. This file, if you remember from the prior article, focuses on text formatting. Not all of the functions are likely to be used, but I am going to dig into several of them. Here is a list of all the functions in the file as of Genesis 1.6
- genesis_truncate_phrase()
- get_the_content_limit()
- the_content_limit()
- genesis_rel_nofollow()
- genesis_strip_attr()
- genesis_tweet_linkify()
- g_ent()
- genesis_formatting_allowedtags()
So what do these do and how can you use them?
genesis_truncate_phrase($phrase, $max_characters)
This function is essentially a helper function used by genesis_get_content_limit(). It has two arguments that are required. $phrase is the text to be limited and $max_characters is the maximum characters allowed in the text. You might consider using this any time you want to limit the text. For example, I have used it for a custom menu I built for a client that puts a thumbnail with the title in a drop down under a custom taxonomy. Since the titles can get really long I had to trim them up if they got too long (really who need a 100+ character title?). Here is some code that does that
echo genesis_truncate_phrase( get_the_title(), 100 );
get_the_content_limit($max_char, $more_link_text = ‘(more…)’, $stripteaser = 0)
This is the function that returns the content after trimming it. The other function, the_content_limit(), is identical except is automatically echos the value saving a bit of code. Both functions take up to 3 arguments. The first, $max_char, is required. It sets the max number of characters allowed in the content before it is trimmed. The second two are optional, they have default values if nothing is passed along. $more_link_text is the link anchor text for the “more link.” The default is “(more…).” The $stripteaser content before the more text, so a … that isn’t linked might be added there. It’s default is “0” which is a “null” value, nothing will be output.
The function retrieves the content with get_the_content() then strips the tags. This is important, even if it is frustrating, because is prevents errors and provides an accurate count. The errors are open tags. For example, if you have a link right smack in the middle of where the text is cut off, then you may have an open anchor tag. The same is true for other html elements. After the content is stripped and otherwise prepared it is trimmed using genesis_truncate_phrase, then the more link is build if available.
This is where the first filter comes up. “get_the_content_more_link” will let you change this link. The next filter is just before the content limit is returned; “get_the_content_limit” lets you alter the final output, you could use this to wrap it in a div, inject code before after or even in the middle, or move the $link to the beginning. the_content_limit gives you one more shot at filtering the output with “the_content_limit.” Though you won’t be able to access the $content, $link, or $max_char variables on that filter.
Here is how you might use it
printf( '<div class="post-teaser">%s</div>', get_the_content_limit( 300, '[Keep Reading]' ) );
This would put the post content, stripped to no more than 300 words, within the post-teaser div.
genesis_rel_nofollow($xhtml)
A simple function to create no follow links.
genesis_strip_attr($xhtml, $elements, $attributes, $two_passes = true)
This is a pretty complex function, it takes any $xhtml text and then uses the provided patterns to remove attributes from specific elements. The $elements and $attributes can be passed as an array or string. For an example, look at the genesis_rel_nofollow() function. This is used to remove any “rel” attributes from the anchor tags before the wp_rel_nofollow() function is used. This prevents the rel value from being added twice if it happens to be in the value being edited already. This same function might be used to strip titles like
$link = '<a href="http://example.com" title="title">Anchor Text</a>'; echo genesis_strip_attr( $link, array( 'a' ), array( 'title' ) ); // outputs "<a href="http://example.com">Anchor Text</a>"
genesis_tweet_linkify($tweet)
Used by the Latest Tweets widget to add link to the tweet hash or reply values.
g_ent( $text = ” )
Added to allow the g_ent filter to change instances of given strings. This is used in the get_the_content_limit() read more link for the ellipse that is inserted before the more link.
genesis_formatting_allowedtags()
Returns the allowed tags for stripped items. Has a filter that lets you change the values to allow more tags or remove those tags.
I hope this has helped clarify some of the functions. I’ll be digging into the image functions next. While you wait, be sure to subscribe to the feed or signup for the feed to be delivered to your email by putting your email in the sidebar widget. Don’t forget to put together your ideas for the Child Theme Creation Wish List.
Boomboom says
Excellent Post again. Lots of useful functions. Please keep them coming 🙂
romy singh says
hi nick,
please help me out… i want to display post excerpt in my genesis child theme without loosing post formatting just like copyblogger…. but i don’t know how to do it…..
nickthegeek says
Thanks for taking the time to comment. I don’t field support questions in my post comments, but if you start a new thread in the StudioPress Support forums, I’ll likely be the one to answer it there.
jipeus says
Dude! this is what i’ve been looking for! you have just save my day, thanks alot