Conditional Tags
From ThaiiS Note (Wiki)
Contents |
A Single Post Page
is_single()
When any single Post page is being displayed.
is_single('17')
When Post 17 is being displayed as a single Post.
is_single('Irish Stew')
When the Post with Title "Irish Stew" is being displayed as a single Post.
is_single('beef-stew')
When the Post with Post Slug "beef-stew" is being displayed as a single Post.
is_single(array(17,'beef-stew','Irish Stew'))
Returns true when the single post being displayed is either post ID 17, or the post_name is "beef-stew", or the post_title is "Irish Stew".
is_single(array(17, 19, 1, 11))
Returns true when the single post being displayed is either post ID 17, post ID 19, post ID 1, or post ID 11.
is_single(array('beef-stew', 'pea-soup', 'chili'))
Returns true when the single post being displayed is either the post_name "beef-stew", post_name "pea-soup" or post_name "chili".
is_single(array('Beef Stew', 'Pea Soup', 'Chili'))
Returns true when the single post being displayed is either the post_title "Beef Stew", post_title "Pea Soup" or post_title "Chili".
A PAGE Page
This section refers to WordPress Pages, not any generic webpage from your blog.
is_page()
When any Page is being displayed.
is_page('42')
When Page 42 (ID) is being displayed.
is_page('About Me And Joe')
When the Page with a post_title of "About Me And Joe" is being displayed.
is_page('about-me')
When the Page with a post_name (slug) of "about-me" is being displayed.
is_page(array(42,'about-me','About Me And Joe'))
Returns true when the Pages displayed is either post ID 42, or post_name "about-me", or post_title "About Me And Joe". Note: the array ability was added at Version 2.5.
A Category Page
is_category()
When any Category archive page is being displayed.
is_category('9')
When the archive page for Category 9 is being displayed.
is_category('Stinky Cheeses')
When the archive page for the Category with Name "Stinky Cheeses" is being displayed.
is_category('blue-cheese')
When the archive page for the Category with Category Slug "blue-cheese" is being displayed.
is_category(array(9,'blue-cheese','Stinky Cheeses'))
Returns true when the category of posts being displayed is either term_ID 9, or slug "blue-cheese", or name "Stinky Cheeses". Note: the array ability was added at Version 2.5.
in_category('5')
Returns true if the current post is in the specified category id.read more Note: Be sure to check your spelling when testing, "is" and "in" are a big difference.
Usage
<?php
if ( is_page('about') || $post->post_parent == '2' ) {
// the page is "About", or the parent of the page is "About"
$bannerimg = 'about.jpg';
} elseif ( is_page('learning') || $post->post_parent == '56' ) {
$bannerimg = 'teaching.jpg';
} elseif ( is_page('admissions') || $post->post_parent == '15' ) {
$bannerimg = 'admissions.jpg';
} else {
$bannerimg = 'home.jpg'; // just in case we are at an unclassified page, perhaps the home page
}
?>
Usage 2
<style type="text/css" media="screen">
body { background : #<?php
if (is_home()) {
echo "000"; }
elseif (is_front_page()) {
echo "000"; }
elseif (is_single() || is_page()) {
echo "fff"; }
else {
echo "555"; } ?> url(images/bg.png) repeat-x; font : normal 0.625em/1em Arial, Helvetica, sans-serif;
</style>
Usage 3
<style type="text/css">
#main-head {
background-image: url(<?php bloginfo('stylesheet_directory'); ?>/images/<?php
if (is_page('about-chiida')){echo "about-chiida.jpg"; }
elseif (is_page('chiida-spa')){echo "best-western.jpg"; }
elseif (is_page('worldwide-office')){echo "pic-inside.jpg"; }
elseif (is_page('services-reservations')){echo "pic-inside.jpg"; }
elseif (is_page('products')){echo "pic-inside.jpg"; }
elseif (is_page('contact-us')){echo "pic-inside.jpg"; }
else {echo "pic-inside.jpg"; } ?>)!important;
}
</style>
Usage 4
<style type="text/css">
<?php if (is_page('about-chiida'))
{echo "#in2{background-image: url(in-about-over.png);}";} ?>
<?php if (is_page('chiida-spa'))
{echo "#in3{background-image: url(in-chiida-over.png);}";} ?>
<?php if (is_page('worldwide-office'))
{echo "#in4{background-image: url(in-worldwide-over.png);}";} ?>
<?php if (is_page('services-reservations'))
{echo "#in5{background-image: url(in-service-over.png);}";} ?>
<?php if (is_page('products'))
{echo "#in6{background-image: url(in-product-over.png);}";} ?>
</style>
Usage 5
array
if (is_page(array(20,173,175,177))){echo "worldwide-office.png"; }
page & child
if (is_page('services-reservations')|| $post->post_parent=='25'){echo "service-res.png"; }
if (is_page('services-reservations')|| $post->post_parent=='25'|| $post->post_parent=='179'|| $post->post_parent=='181'){echo "service-res.png"; }
Unique Single template
<?php
$post = $wp_query- >post;
if ( in_category('1') ) {
include(TEMPLATEPATH . '/single1.php');
} elseif ( in_category('2') ) {
include(TEMPLATEPATH . '/single2.php');
} else {
include(TEMPLATEPATH . '/single_other.php');
}
? >
<?php if ( is_home() ) { include ('file.php'); } ?>
http://www.webdesignerwall.com/tutorials/wordpress-theme-hacks/
on OB
write it on archive.php
<?php
if ( is_category('16')){ include(TEMPLATEPATH . '/archive1.php');}
else { include(TEMPLATEPATH . '/archive2.php');}
?>
or array
<?php
if ( is_category(array(16,21,17,19,20,14,18))){ include(TEMPLATEPATH . '/shop.php');}
else { include(TEMPLATEPATH . '/products.php');} ?>
condition with php tag
<?php if(is_front_page()||is_category() ){ ?>
<div class="eachpost twocols"><?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); } ?></div>
<?php } else { ?>
<?php } ?>
on top menu GT
<?php if (is_home()){echo "gt-main-nav_01a.png"; }else{echo "gt-main-nav_01.png";} ?>
<?php if (is_page('corporate-information')){echo "gt-main-nav_02a.png"; }else{echo "gt-main-nav_02.png";} ?>
<?php if (is_category('service')){echo "gt-main-nav_03a.png"; }else{echo "gt-main-nav_03.png";} ?>
<?php if (is_category('news')){echo "gt-main-nav_04a.png"; }else{echo "gt-main-nav_04.png";} ?>
<?php if (is_category('promotions')){echo "gt-main-nav_05a.png"; }else{echo "gt-main-nav_05.png";} ?>
<?php if (is_page('customer-service')){echo "gt-main-nav_06a.png"; }else{echo "gt-main-nav_06.png";}v
<?php if (is_page('site-map')){echo "gt-main-nav_06a.png"; }else{echo "gt-main-nav_06.png";} ?>
on ELCT
in blog and is blog
<?php if (is_category('blog') || in_category('blog')) {echo "elct_16a.jpg"; } else {echo "elct_16.jpg";} ?>
