Often it is desirable to put a new section into a site, but only in certain places or under certain circumstances. This is a relatively simple process and may be done via a theme function, following the tutorial on How to Add a Section to Your Site Using Hooks or from Genesis Simple Hooks.
For both of these examples I will just display “hello world” before the loop so I need the genesis_before_loop hook. If I wanted to display this elsewhere I would use another hook. I also need to know the conditional tags built into WordPress. The most common ones are
is_home() – returns true on home page
is_front_page() – returns true on front page
is_singular() – returns true on single post or page
is_page() – returns true on single page
is_single() – returns true on single post
in_category() – returns true if post is in a category
has_tag() – returns true if a post has a tag
is_category() – returns true on category
is_archive() – returns true on any archive
You will see that there are a lot of other options in the WordPress codex article including additional arguments for selecting specific categories, pages, posts ….
Now using them with hooks couldn’t be easier. If you want to put “<h3>Hello World</h3>
” at the top of all of your pages then you could paste that into your function, or into the genesis_before_loop field of Genesis Simple Hooks.
What if you only want this to show up on the home page? Then put in a conditional tag. This is what you would paste into your function when adding a section with hooks.
if(is_home()) { ?> <h3>Hello World</h3> <?php }
The only difference between Genesis Simple Hooks and the add to site via hooks method is you need to enable php on the hook and in your code. So if I were using Genesis Simple Hooks, this is what I would paste into the field to have my code only show up on pages.
<?php if(is_page()) { ?> <h3>Hello World</h3> <?php } ?>
This would work the exact same for any post, page, category … so long as there is a conditional tag for it, but what if I want this to show on posts in category “foo” or on a page? Use this code (don’t forget the php tags if using GSH)
if(is_page() || in_category('foo')) { ?> <h3>Hello World</h3> <?php }
The “||” is like saying “or” so the code will run if either of those tags return true, but sometimes you want something to run if both tags are true. Maybe you have posts that are in category “foo” and are tagged with “bar.” You can do this:
if(in_category('foo') && has_tag('bar')) { ?> <h3>Hello World</h3> <?php }
This will only show Hello World on posts in the category that also have the tag. “&&” is like saying “and.”
Finally, there are times you want to add something to your site everywhere but a specific part of the site. For example, you want to say Hello World on pages, posts, categories … but not the home page. Then make this small change to the first code:
if(!is_home()) { ?> <h3>Hello World</h3> <?php }
That little “!” tells the code to return the opposite of normal. Another way of thinking of it is “!” = “not.” Our conditional tag is saying “If this is not the home page then echo Hello World.”
The conditional tag is very powerful and can be extended to long complex strings. Review the Codex article for more details.
Paul says
I think you meant is_front_page() – missing an underscore.
I do this very often. Plugins like Spots allow you to create sections to insert in specific places, with maximum flexibility for the user.
http://wordpress.org/extend/plugins/spots/
Or you could maybe create a custom post type with some restrictions.
nickthegeek says
Paul, thanks. I fixed it in the post.
kathoey says
what if i want to only show an ad in the home page and single post only, same ad code and spot. what should be the php code?
nickthegeek says
you would use if( is_home() || is_single() ), you can use || (which means “or”) and && (which means “and”) to build increasingly complex conditionals.
Jonathan Wilson says
Great post, Nick!
Question: What if I only wanted “Hello World” to appear on certain pages, like only the About Us and Portfolio pages? How would I specify which pages?
nickthegeek says
Jonathan,
The is_page() function accepts several arguments that you can use to target specific pages
http://codex.wordpress.org/Function_Reference/is_page
Jonathan Wilson says
Thanks!
Dave says
Nice post! I’ve been looking to put ads only on specific category pages and was hitting a wall, this was the solution I was searching for.
Nate W. says
Nick,
This is a lifesaver man. I was trying to make it so advertisements didn’t show in a certain category and this did the trick! I didn’t even have to spend 7 hours spinning my wheels.
You are one of the reasons the Genesis framework is so useful. Thanks!
John Krytus says
I can’t believe I’ve never used hooks before! This made it really easy. Thanks for the help.
Cotton Rohrscheib says
Hi, thanks for this post, this helps a lot. I have ran into an issue though that I can’t figure out and was wondering if you could help. I have a shortcode that I want to run on the front page (home) of a website. I have inserted the following into the genesis_before_loop hook:
[slideshow gallery_id="1"]
Before I added the is_front_page php in I could see the gallery (from the shortcode) on all pages, now I see it on none. Any suggestions?
Thanks for your help in advance.
Cotton
Alex says
It only works for me in GSH if the code begins and ends thusly:
Hello World
Without the
Laurie Gaudino says
So easy – when explained correctly!
Thanks.
Laurie
Andrew Walsh says
Great! I was looking around for exactly this (trying to put in some extra content only above one category of post) and it just wasn’t working. I wasn’t including the php closing tag on the same line as the conditional statement, and I’m guessing that’s why it didn’t work. Thanks!
Angie Lambert says
I so wish I would have found this post about a month ago because this is exactly what I was looking for! However, I do have a question for you, I have a custom call to action and I don’t want it to show on pages or the home page but I do want it on the blog page after each individual post. I have a tried a couple of ways, right now and nothing is working. This is what I had and it removed it all together:
(!is_home()&& !is_page()&& is_page_template( ‘page_blog.php’ ))
Do I need an OR statement?
nickthegeek says
Angie,
If your blog page is using the blog template just use if( is_page_template( ‘page_blog.php’ ) ) { }
Chris Becker says
Nick, you are the man! These are some great directions and have helped me resolve a client’s need to have different Google Analytics codes show up on different pages. Thanks so much!
Mark Asacker says
Hey Nick,
Your tutorials are always spot on. I do have a snag though…
I’m trying to add a navigation menu to certain pages so I’m doing this…
MEET THE CHEF
SCHOOL HOUSE HISTORY
It works in regards to preventing the menu from showing up on all other pages, however…
The menu only shows up on page ID 98 and not on page ID 108 or 110?
I reviewed the codex you referred to and the only discrepancy I see is that it states this won’t work inside The Loop.
I’m using the “genesis_before_post_title” Hook which IS inside the loop.
I figure it wouldn’t show up on page ID 98 if it didn’t work inside the loop.
Is that the problem or am I missing something completely?
I would really appreciate any further direction you can give me.
Please bear in mind I’m not proficient at php or genesis hooks. I read a lot in the forums and look to superstars like you for reference.
Thanks Nick!
Mark Asacker says
Tried moving it to the “genesis_before_loop” Hook but it’s still doing the same thing.
Only displaying page ID 98 and NOT 108 or 110.
Thanks again Nick!