Conditional Tags

Conditional tags can be used in any template, but they're especially useful in the include files—the header, sidebar, and footer—where using conditional tags let you handle alternatives without creating separate files. Table 6-2 lists the conditional tags and their arguments. In the next few sections, you'll see several ways of using these conditional tags.

There are four classes of conditionals: is*, in*, has*, and *open. Is_sticky() and is_paged() refer to properties of individual posts or pages. All the other is* functions are true if the currently viewed page is of that type. For example, is_single() returns true if it appears in a single post archive template. The is_front_page() function returns true if you're viewing the site's home page, whether you've set the Reading options to display blog posts or a single page, whereas is_home() is true only on the main blog posts page.

The in*, has*, and *open functions refer to properties. For example, comments_open() is true if displayed on the archive page for a post that allows comments. The in_category() function is true only if a post has been assigned directly to the category in question; the function does not check subcategories. (You'll make use of this trait just a few pages from now, when you want to separate subcategory posts in the category archive template.)

Like all WordPress template tags, conditionals are really just functions. Most of these functions can be used inside the Loop or in a specific archive template without arguments. Outside the Loop—that is, in an advanced theme function like you'll see in the next chapter, or in a plugin—you need to provide some identifying information about the post, page, author, tag, category, or taxonomy term you're interested in. Table 6-2 lists the various arguments that each function accepts.

Table 6-2. Arguments accepted by the conditional tags

Conditional

ID

Slug

Title

Array

Other

is_single

X

X

X

X

is_sticky

X

is_page

X

X

is_page_template

Filename

is_category

X

X

X

X

in_category

X

is_tag

X

X

X

has_tag

X

X

X

is_tax

X

X

X

is_author

X

Username

Nickname

X

has_excerpt

X

The following tags do not accept arguments; they are true if the corresponding archive template is being used to display the current page.

is_home

is_search

is_day

is_feed

is_404

is_comments_popup

is_front_page

is_time

is_month

is_attachment

is_trackback

is_active_sidebar

is_archive

is_date

is_year

is_singular

is_preview

is_admin

The following tags do not accept arguments. They return information about the currently viewed page.

comments_open

pings_open

is_paged

in_the_loop

0 0

Post a comment