It’s no secret: I love WordPress.
I’ve been using it more and more as a content management system (rather than just a blogging platform) and the more I dig into some of the functions in WP, the more awesome it becomes.
For example: Conditional Tags.
They seem simple enough. About a dozen functions that check to see if a certain condition is true and return a Boolean (TRUE or FALSE).
For example is_admin() checks to see if your Dashboard panels are displayed. This function is frequently used when enqueuing scripts in WP. You don’t want to load tons of extra JavaScript or Stylesheets into your page if you’re viewing the Dashboard. Each millisecond of load time is precious. So do something like this.
[cc lang=”php”]
if ( !is_admin() ) {
//If the user is NOT (!) on the admin page (Dashboard).
wp_register_script(‘myscript’, get_bloginfo(‘stylesheet_directory’) . ‘/js/myscript.js’, array(‘jquery’) );
wp_enqueue_script(‘myscript’);
}
[/cc]
Enqueue your script.
This is probably the most common use of conditional tags I’ve seen.
Additionally these functions are used frequently to exclude content being indexed by Search Engines.
[cc lang=”php”]
if ( is_category(‘personal’)) {
}
[/cc]
Here’s we’re excluding the category named “personal” from being indexed.
Let’s look at a more complex (and hopefully very practical example).
Say you use WordPress to post events and headlines on your church site and/or blog. You might be getting to the point where you are posting so often, your events feed is getting cluttered and you want to create separate pages that have more specific posts on them.
We’ve set up a page template with a list of posts in a specific category being queried as follows:
[cc lang=”php”]
- have_posts()) : $headlines->the_post();?>
- have_posts()) : $headlines->the_post();?>
Jared Erickson says
you forgot
$highfive = get_option(‘highfive’);
if (is_tom(‘the_man’)) {
echo ‘give him a’ . $highfive;
}
Tom McFarlin says
More like is_brian_notess(‘the_man’); ..!
Brian Notess says
More like
if ( is_awesome(“8BIT”) {
give_high_fives(“Tom”, “Jared”);
}