If you would like to talk about your project call (02) 4474 5861

Using Conditional Statements with WordPress Conditional Tags

By using conditional statements in WordPress you can have custom text, images or links etc to appear on specific categories, pages or posts.

The basics of a conditional statement looks like this:

<?php if ( ) { ?>
<?php } else ( ) { ?>
<?php } ?>

or like this if you are not going to run other php:

<?php
if () {
echo "if this is true";
} else {
echo "the above was not true";
}
?>

WordPress Conditional Tag is_page()

Here is an example of targeting a page within WordPress that has an id of 8 which will then display the a <h3> custom heading:

<?php
if ( is_page(8) ) { ?>
<h3>Yes this the page with an of ID8</h3>
<?php } else { ?>
<h3>No this is NOT the page with an of ID8</h3>
<?php } ?>

The following targets child pages of a parent page which has an id of 8 which will then display the a <h4> custom heading:

<?php
if ( $post->post_parent == '8' ) { ?>
<h4>Child pages heading for parent page with ID8</h4>
<?php } else { ?>
<h4>Not a Child page of ID8</h4>
<?php } ?>

or the above two examples could be combined like this:

<?php
if ( is_page(8) ) {
echo "<h3>Page with ID8 Heading</h3>";
} elseif ( $post->post_parent == '8' ) {
echo "<h4>Child pages heading for parent page with ID8</h4>";
} else {
echo "<p>None of the top two conditions are true</p>";
}
?>

This next example when added to a page.php in place of the standard get_header(); tag, the following code will load a custom header file named header-shop.php when displaying the page http://yoursite.com/products

if ( is_page('products') ) {
get_header( 'shop' );
} else {
get_header();
}

WordPress conditional tags are not limited to one page. You can make several statements in a row like the following example

if ( is_page('products') ) {
get_header( 'shop' );
} elseif ( is_page( 42 ) ) {
get_header( 'about' );
} else {
get_header();
}

View the complete list of WordPress Conditional Tags

Instinctively creative & thoughtful strategies with result orientated outcomes

Visually communicating effectively with your potential & current customers adds to the good business experience.

BJ2DESIGN combines a wealth of design knowledge with creative business strategies to create result oriented work.

Read More About BJ2DESIGN

creative hand and tools