Conditional Shortcodes WordPress Plugin

Conditional Shortcodes is a WordPress plugin that gives the power of conditional tags to the people writing and editing the content.

As theme designers know, you do some things differently when showing one post than when showing many, and WordPress has tools for handling this. First, there are the different theme files (e.g. single.php for single posts), including the ability to have a special theme file for each post or category if desired. In addition, there are the conditional tags, functions such as is_single and is_archive, for more surgical changes.

But all of that flexibility goes to the theme designer. In general, people on the content creation side have maybe two tools for changing the display of things. They have the <!–more–> tag to limit what gets shown outside of the single-post page. Alternatively, if the theme uses it, they can provide a custom excerpt for those other pages. That’s it.

This plugin gives the content developers a set of shortcodes that do (somewhat) the same things as many of the conditional tags. The codes are:

Conditional Tag Shortcode Includes content when…
is_single() [is_single] showing only a single post.
is_singular() [is_singular] showing only a single post or page.
is_home() [is_home] showing the blog home page.
is_front_page() [is_front_page] showing the front page of your site.
is_sticky() [is_sticky] the current post is considered “sticky”.
is_category() [is_category] showing a category archive.
is_tag() [is_tag] showing a tag archive.
is_tax() [is_tax] showing a category or tag archive.
is_author() [is_author] showing an author archive.
is_archive() [is_archive] showing any archive.
is_year() [is_year] showing a yearly archive.
is_month() [is_month] showing a monthly archive.
is_day() [is_day] showing a daily archive.
is_time() [is_time] showing a hourly or shorter increment archive.
is_feed() [is_feed] generating a feed.
comments_open() [comments_open] comments are open on the current post.
is_search() [is_search] showing search results.

For example, suppose you want to include a download button only on a single post page. In your post, you could write:

[is_single]
<a href="yourdownload url">DOWNLOAD HERE</a>
[/is_single]

Each conditional shortcode also has a sort of “else” short code for when the condition is not true. These are the same as the shortcodes, but with not_ instead of is_. (The “else” code for comments_open is not_comments_open.) These don’t have to be closed as they are considered part of the positive shortcode.

To continue the example, you might want to let people know they can download from the full post:

[is_single]
<a href="yourdownload url">DOWNLOAD HERE</a>
[/not_single]
See full article to download.
[/is_single]

Note that you don’t have to put anything in the first section to use the “else” part, so you can use this this as an idiom for “if not” with the tags. For example, you could not that comments are closed like so:

[comments_open]
[not_comments_open]
We're terribly sorry, but we are not longer accepting comments on this article.
[/comments_open]

These shortcodes can also be nested for more complex logic as needed. If you think of a good example to put here, let me know.

Note that these shortcodes do not emulate all of the behavior of the conditional tags that can be used in themes. Specifically, they do not take any arguments, while many of the tags do. Of course, in some cases the arguments would simply be redundant, such as the arguments to is_single to determine which post is showing. If people really want the arguments, let me know and I will add them in a future release.

Related Posts

  • Chapter Quotes for WordPress Posts Revisited
    A few months ago, I wrote a post about adding chapter quotes to your WordPress posts. Here I'll show you another way to add them to posts without them showing up outside of the single-post page, and ...
  • The Custom Rel WordPress Plugin
    BlueHostDreamHostGoDaddyJustHost Starting with version 2.8, WordPress automatically puts relational link tags in the heads of posts. This is not a bad thing at all. Not only are they st...
  • The Atropos WordPress Plugin
    [17-Apr-10 Updated to fix error message noticed in 2.9.2 (but possibly there all along).] [08-Jun-09 Updated to note 2.7.1 compatibility.] The Atropos plugin lets you set an expiration...

Leave a Reply

HostMonster for Great Unlimited Web Hosting Hundreds of template and themes from $35