WordPress if is page-class. Conditional tags to target archives, tags, categories

This article was written by in July 28, 2015, & may not be posted on other sites!
Original source url for this article: WordPress if is page-class. Conditional tags to target archives, tags, categories

WordPress if is page-class. Conditional tags to target archives, tags pages, category pages.
There are a lot of different conditional tags to use when working with WordPress templates or theme files. To target specific pages by body class, or an archive for a clicked tag or category keep on reading. This is how to use conditional tags to target archives, tags, categories or any other type of page in WordPress.

Wordpress if is page-class. Conditional tags to target archives, tags, categories

 

WordPress if is page-class function. Conditional tags to target archives, tags or categories

Check if is page archives > Any tag
Meaning if user clicked on any tag and is now on the archives page
<?php if ( is_tag() ) : ?>
// your code
<?php endif; ?>

Check if is page archives > Some tag
Meaning if user clicked on a tag and is now on the archives page of that tag.
<?php if ( is_tag('Some tag') ) : ?>
// your code
<?php endif; ?>

Check if is page archives > Any category
Meaning if user clicked on any category and is now on the archives page
<?php if ( is_category() ) : ?>
// your code
<?php endif; ?>

Check if is page archives > Some category
Meaning if user clicked on a category and is now on the archives page of that category.
<?php if ( is_category('Some category') ) : ?>
// your code
<?php endif; ?>

 

Other conditional tags to target specific pages in WordPress

Check if is page by Page ID
Meaning if is page with ID 15
<?php if ( is_page( 15 ) ) : ?>
// your code
<?php endif; ?>

Check if is page by template
Meaning if page is using a special template
<?php if ( is_page_template( 'my-template.php' ) ) : ?>
// your code
<?php endif; ?>

Check if is page is a single post
Meaning if user click to open a single post on its own page
<?php if ( is_single() ) : ?>
// your code
<?php endif; ?>

Check if is page is a single post displaying post with ID 15
Meaning if user click to open a single post on its own page, and this post hade id 15
<?php if ( is_single('15') ) : ?>
// your code
<?php endif; ?>

 

Spread the word

Facebooktwittergoogle_pluslinkedinmail

Leave a Reply

Your email address will not be published. Required fields are marked *