Hook Display Conditionals

As you probably already know, hooks are a wonderful way to easily add content to various parts of your website. As stated in the Hooks Guide, manually adding hook content with PHP provides maximum flexibility to your hooks.

Not everyone wants to dive into coding, though. So instead of making you choose between flexible and inflexible features, the built-in hooks interface was designed to be flexible as well.

Built-in Hook Conditionals

There are seven different conditions that you can use to display your hooks using the Volatyl Options. They are:

  • Blog
  • Front Page
  • Posts
  • Pages
  • Archives
  • Search
  • 404

By default, if you place content inside of a hook using the Volatyl Options, that content will display in all areas where that hook is set to be displayed.

The options are designed for you to simply hide the content based on the aforementioned conditions. Let’s break down each of the conditions.

Blog

The blog is simply wherever your article feed is being displayed. For most users, this is the homepage of your WordPress website. For others, you may have a static page set as the homepage (Dashboard => Settings => Reading => Front Page Displays). If that is the case, whatever page you set for the “Posts Page” will be the blog conditional.

Front Page

The front page of your WordPress install is just that… the front page. If your article feed is the homepage of your website, it is also considered the front page. If a static page has been set as your homepage, as described above, that static page becomes your front page.

Posts

This includes all single Posts added through your WordPress dashboard.

Pages

This includes all Pages added through your WordPress dashboard.

Archives

Archives include a number of built-in WordPress taxonomies. This condition controls hook content on category, tag, author, date, and similar taxonomy pages. For most users, categories and tags are the most notable.

Search

This is the page that displays search results.

404

In the event a visitor attempts to view a page on your website that does not exist, they will be sent to a 404 error page.

Using Volatyl Hook Conditionals

To make things easier to use for users of every technical skill level, there are two ways to take advantage of these conditionals.

The first and easiest way to do so is through the Volatyl Options.

Volatyl Hook Conditionals

In your WordPress dashboard, navigate to Dashboard => Appearance => Volatyl Options => Hooks (tab) and beneath each textarea for all hooks, you’ll see the checkboxes for hiding hook content.

Screen Shot 2013-03-10 at 4.00.56 PM

If you check the box for “Posts,” for example, your hook content will display on every part of your website that it is supposed to display on except for single posts. Too easy.

The “Disable Hook” checkbox overrides all hook conditionals.

Manual (PHP) Hook Conditionals

The built-in conditional options were simply an attempt to mimic the flexibility that writing raw PHP provides. However, they don’t exactly match up pound for pound.

With PHP, not only can you add content (custom functions) to all Volatyl hooks based on the conditions described earlier in this article, but you may also get even more specific.

What if you only wanted your headliner to show on one specific page? Using the built-in conditionals and checking everything but “Pages” would certainly display the headliner where you wanted it. But there would be no way to prevent every other page on your site from displaying the same content. Bummer.

That’s where writing raw PHP comes in.

In the Volatyl Hooks Guide, we talked about how to manually add content to your hooks.

It’s very easy. Here’s the example we used to add content to our headliner (we added this code to our child theme’s functions.php file):

// Add content to Headliner
function custom_headliner_content() { ?>
	<p>This is some coooool stuff!</p>
<?php }
add_action( 'vol_headliner', 'custom_headliner_content' );

Again, revisit the Volatyl Hooks Guide for a break down of that code.

To use conditionals, all you have to do is insert a little bit of information into your custom function. Based on the aforementioned need to have content placed only on one specific page, here’s what the finished function would look like with a conditional added to it:

// Add content to Headliner
function custom_headliner_content() { 
	if ( is_page( 'about' ) ) { ?>
		<p>This is some coooool stuff!</p>
	<?php }
}
add_action( 'vol_headliner', 'custom_headliner_content' );

Take a look at the conditional statement beginning with the word “if.” It basically reads, “if the current webpage being served is a WordPress Page with a slug of “about” then run the code within the following brackets.”

This code would only show up on that particular Page as opposed to all other pages.

When you manually add hooks using PHP, the built-in conditional checkboxes do not apply to your custom function. However, using the “Disable Hook” checkbox will override your custom hook.

Slugs are not the only tool that can be used to specify conditions. Using the WordPress Codex Conditional Tags page, you can use any combination of conditions to customize your site.

In the Create Custom Page Layout guide, we used a Post ID to target a specific page that we were using to build our homepage. The same capabilities apply here.

Use the multiple resources you have been given to customize your site. If you don’t need to get too fancy, the built-in hook conditionals may very well be enough.

Enjoy! 🙂

Return to docs