Thord Daniel Hedengren - Smashing WordPress_ Beyond the Blog-Wiley (2014)

(avery) #1

CHAPTER 4 • WordPress Theme Essentials 103


Say you want to encourage your readers to subscribe to your RSS feed. You want to output a div
with the class promotion, and within it an h4 header and a line of text. Thanks to the magic of
CSS, you can style it graphically any way you want just by applying styles to the div — maybe
something like this:


div.promotion {
background: #eee;
border: 1px solid #bbb;
padding: 10px;
}


div.promotion h4 {
color: #f00;
font-size: 14px;
margin: 0 0 5px 0;
padding: 0;
}


div.promotion p {
font-size: 12px;
color: #444;
margin-bottom: 0;
}


That would probably look decent. To really bling it up, you should add a background image
featuring a nice RSS graphic or something to the div, but forget about that for now. This is
the full HTML you want to output after your marvelous posts:



Never miss a beat!



The smashing keeps
you up-to-date!



How, then, do you get this thing to output itself without hacking the template files? Easily
enough: You can use functions.php and attach it to the the_content filter hook. (You
know, the one that outputs the actual content, after which you want to add it.) Here’s the
functions.php code:


function Promotion( $content ) {
if(! is_feed() &&! is_home() ) {
$content.= '

';
}

Free download pdf