Posted: May 18th, 2010    By: Devin Walker   

The WordPress loop is extremely useful when developing customized WordPress solutions. One bit of code I've had to use often is one that will display a set of code if the page or post is a parent of child of a certain category. This is an easy way to display, output, or show data on all of your WordPress pages that are a parent or child of a specific category.

What the Code Looks Like

If you are familiar with PHP and the WordPress Codex then you'll know that typically you could use conditional tags to detect the page in a conditional statement. This code is separate from the WordPress codex and is just pure server side PHP goodness.

Display Info on Parent and Child Pages

Here's an example of a WordPress Conditional Statement that displays any relevant information if the particular page is named "Foo" and the category is 420.

<?php if(is_page('Foo') || $post->post_parent == '420' ) : ?>
<?php endif; ?>

Used inside the WordPress Loop


<?php if(is_page('Foo') || $post->post_parent == '420' ) : ?> 
//here we start the conditional statement
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
//here we start the wordpress loop 
<a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a>	
//this grabs all post titles and links                        	 
<?php endwhile; else: ?>       
<?php endif; ?>
 //end wordpress loop
<?php else: ?>
//do something else (not necessay but useful)
<?php endif; ?>
 //end if 

The above example is a bit more complex but makes great use for showing the information you want, where you want on your WordPress site.

Display Info on Parent and Child a WordPress Category

The first examples written above dealt with WordPress pages. Now let's show how do display some data using the WordPress conditional tag in_category()

If Post is in Category

This time around we make use of the in_category() conditional tag. In this example some data will show if the category is Foo.

<?php if(in_category('Foo')): ?>
// my data
<?php endif; ?>

If Post is in Category do WordPress Loop

Here we want to display data through the wordpress loop if in a category

<?php if(in_category('Foo')): ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>// my data
<?php endwhile; else: ?>
<?php endif; ?>
<?php endif; ?>

Summary

Using the examples above you can create cool customized sidebars and display information across your website in a more dynamic way. Please let me know if you have any problems by using the contact form below!

Related posts:

  1. How to Use a Facebook Like Button on Any Page The facebook like button is in. I know, you've been seeing it more and more around the web and now you want to implement it on your own website. I'll show you how to do just this using a little snippet of code that will allow you to add a dynamic Facebook "like" button to [...]...
  2. WordPress Heirarchical Sidebar Page Menus Let's say you want to show a hierarchical sidebar menu that displays a listing of current subpages. This is easy enough, but let's say that you have a top menu that shows all top-level pages. Once you click on a top level page from that menu it will take you there and in the sidebar [...]...
  3. WordPress Alternating Classes It's often very useful to have alternating classes in for styling purposes. Let's say you want every other row to be a darker shading, this is very common and I'll show you how to implement this in WordPress using a simple bit of code. For my example here I will show you how to create [...]...
  4. WordPress List Subpages Even if On Subpage I'm building a new site and the navigation on it requires me to use a different bit of code for parent and child pages. I thought I'd share the code for those out there building a similar navigation. Here's a handy bit of code that you can use to display all subpages on a subpage. [...]...
  5. WordPress: If URL is Homepage then do this… Question: In WordPress how to I make sure the page loaded is the homepage URL and display some data if the standard WordPress Conditional Tags are not working for my theme?! Answer: Here's a quick snippet of code that will give you an option to do something if these WordPress basic conditional tags are not [...]...