using is_page() conditional tags in themes

November 2, 2009

How and where to use is_page() conditional tag :

1)  Login to Administrator section

2) Pages > Add New

3) Give title as “About” and publish it.

If page.php is not there in wp-content/themes/current_theme folder, than wordPress will use index.php of the current theme to display the content.

If page.php is there and we want that “About” page should have some different look than other pages, then use the following  to achieve it:

1) Create a template file named “About.php” file same as page.php file in wp-content/themes/current_theme folder

2) Do formatting as per your needs.

3) Edit the page.php file as mentioned below :

get_header(); ?>

// use is_page conditional tag to check whether template file exists for “About” page

<?php if (is_page(‘About’)) { ?>

<?php include(“About.php”); ?>   // if template file exists than include the “About.php” file
<?php } else { ?>

// Other pages will use this section for displaying the content
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content(‘<p>Read the rest of this page &raquo;</p>’); ?>
<?php wp_link_pages(array(‘before’ => ‘<p><strong>Pages:</strong> ‘, ‘after’ => ‘</p>’, ‘next_or_number’ => ‘number’)); ?>
<?php } ?>
<?php get_sidebar(); ?>
<?php get_footer(); ?>

IMP:

The template file name and the name used in is_page() must be same as page title mentioned in creating page in administrator section.

for e.g : if page title is “About” then

1) create template file with name About.php

2) use page title in is_page() as is_page(‘About’)

Entry Filed under: Wordpress. .

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Calendar

November 2009
M T W T F S S
« Sep    
 1
2345678
9101112131415
16171819202122
23242526272829
30  

Most Recent Posts