I was looking through the posts on actions and realized that there was one important tip I left out. If you haven’t read up on the actions already, you should probably start at the beginning. To make it easier I have tagged all of the articles with an “Actions” tag, so check out the Actions archive.
Now that you know what actions are, how to use them to add, move, and remove existing content, new content, and plugins you need one more helpful tip.
Adding Duplicate Content
Whether you are adding a tweet button to the top and bottom or your post, putting some extra markup around several elements, or inserting adsense in more than one place, you need to be able to duplicate your code. One might do this by creating an extra action/function pair, but that isn’t really required, in fact, you can use some conditional code so that very similar code is altered depending on where it is being used, allowing you to have one function that is used in more than one place.
In this first example, I am demonstrating an adsense add being put at the top and bottom of your post content.
add_action( 'genesis_before_post_content', 'child_content_conditional_adsense', 15); add_action( 'genesis_after_post_content', 'child_content_conditional_adsense', 5); /** inserts an adsense ad above and below the content */ function child_content_adsense() { ?> <div class="adsense"> <!--insert adsense code--> </div><!-- end .adsense --> <?php }
Now this exact same code will load just before and after the .entry-content div.
If I wanted to handle the markup differently, then I need to know which hook I’m loading on. This is handled conditionally, which I will explain in more detail in another tutorial. For now check out my article on Dev.SP for using conditional code with actions. This code handles what we need though.
add_action( 'genesis_before_post_content', 'child_content_conditional_adsense', 15); add_action( 'genesis_after_post_content', 'child_content_conditional_adsense', 5); /** inserts an adsense ad above and below the content with a conditional class. */ function child_content_conditional_adsense() { ?> <div class="adsense <?php echo 'genesis_before_post_content' == current_filter() ? 'before' : 'after'; ?>"> <!--insert adsense code--> </div><!-- end .adsense --> <?php }
This doesn’t look like a traditional conditional statement, but it is a very efficient way of assigning a variable value or echoing a statement like this. The first part says it is to echo (that is print that in the code at that location) the next part, up to the “?” tells it the condition it is to look for. The “?” tells it what string to use if the condition is true, in this case of the current_filter() is “genesis_before_post_content” and the code after the “:” tells it what to show when it isn’t on that hook. Now it can be styled as “before” and “after” so you can float one block left and the other right or whatever.
Tim Signore says
Nick – thanks so much for posting all this valuable stuff. I’ve been learning the Genesis framework over the past few weeks. While there is a lot of good info on the dev.sp site, the dots are not always joined between the tutorials. Your series does just that; it’s great to have an overview of the system and understand it’s capabilities better.
Alex v says
This answers a question you helped me with in the studiopress forums. Thanks!
DJ Lein says
Nick, I’m loving the tutorials; they really help me understand the foundation of how Genesis works. That said, I’m struggling to insert “lego blocks” inside the .entry-content div.
Above you say the ‘genesis_before_post_content’ and ‘genesis_after_post_content’ hooks insert content within .entry-content, but the hook reference guide at http://www.studiopress.com/tutorials/genesis/hook-reference says it’s outside.
Which is correct? And how do I insert “blocks” within .entry-content?
nickthegeek says
DJ, I’m looking at the code right now, genesis_before_post_content comes immediately before .entry-content and genesis_after_post_content comes immediately after. I’ll update my post to reflect the change.
To put code inside .entry-content, you need to use the genesis_post_content hook, which is inside the .entry-content. You can use a priority value less than 10 to get your code before genesis_do_content.
DJ Lein says
Right on. Thanks for looking into it and for providing a solution that works.
Keep up the good fight!
Robin Jennings says
Great tutorials you’ve got here Nick. Often the information is far superior than some of Genesis’ own help pages.
Awesome job- I hope it’s leading to more work for you.
vajrasar says
Sorry Nick, for my dumbness but I didn’t seem to know what happened here in –
“The first part says it is to echo (that is print that in the code at that location) the next part, up to the “?” tells it the condition it is to look for. The “?” tells it what string to use if the condition is true, in this case of the current_filter() is “genesis_before_post_content” and the code after the “:” tells it what to show when it isn’t on that hook.”
Plus, what is current_filters() doing here!
Thanks for such a great series.
vajrasar says
well, I got it somehow, just let me know if am right –
if current_filters() will be == genesis_before_post_content, then ‘before’ would execute else ‘after’ will execute. Am I right?
If am right, then would you please tell me a little more about before and after (what they are or would be doing). Thanks.
nickthegeek says
Yes, you have it figured out. Basically it is a short version of
The current filter sets which filter (or action) is being used so you can make your code different on different hooks. In my example it is just a class change, so if you wanted to apply a different width, float, background …. to the ad container before the content and after the content you have classes in place to make it all work.
pduraesa says
Hi Nick I am new here, and I am a complete beginner, I’ve been following all your tutorials in order, I’m very thankful for the enlightenment, since I am essentially a visual/graphic designer wanting to delve into these basics. I am astonished with how far I’ve been able to do up to this point of your tutorials, well thanks to you. But I got stuck with what code should I introduce in the
? Is it some plugin I need to download off the WordPress site? Or is it some plain HTML code embedded in PHP tags? Sorry but my mind just went blank, so your help would be very much appreciated please. (by the way I also have strong Christian beliefs, I thank God for coming across with good natured people like you!)
nickthegeek says
Sorry I’m not entirely sure I follow what the question is referencing. Care to explain in more detail?
pduraesa says
Sorry, the bit I am not sure about is supposed to go inside ()
pduraesa says
Sorry, what goes inside the DIV with the class of ‘adsense’?
nickthegeek says
You add your adsense code there.