WordPress has many nice built in features and helper functions for us to use in our websites. Some of the basic conditional functions like is_page, is_single, and is_home are perhaps the most versatile and useful to developers and site owners. These functions simply return true or false for if you are on a page, post, or the home page respectively. However they allow you to easily define custom sidebars and content blocks depending on where the user is on your site.
A basic example would be that you have decided that on your home page you would like to display a random post to the user. However when the user is looking at a post you want to display a post in the same category of the post being viewed. To accomplish this you can utilize the conditional tags is_single, and is_home. The logic shown below simply tests if the current page being displayed is the home page if so it outputs a random post, otherwise if it is a single page (post) it outputs a post from the same category of the current post being viewed.
<?php
// check if on home page
if (is_home()) {
$rand_posts = get_posts('numberposts=1&orderby=rand');
foreach( $rand_posts as $rpost ) :
?>
<div>
<h3 ><a href="<?php echo get_permalink( $rpost->ID ); ?>" target="_self"><?php echo get_title($rpost->ID); ?></a></h3>
<div><?php echo get_post_meta($rpost->ID, 'teaser', true); ?></a></div>
<a href="<?php echo get_permalink( $rpost->ID ); ?>" target="_self">Learn More Now »</a>
</div>
<?php
endforeach;
// test for single post
} else if (is_single()) {
// get post categories
$categories = get_the_category();
$cats = array();
foreach( $categories as $category ) {
$cats[] = $category->cat_ID
}
$rand_posts = get_posts(array( 'numberposts' => 1, 'orderby' => 'rand', 'category__in' => $cats, 'exclude' => $post->ID) );
foreach( $rand_posts as $rpost ) :
?>
<div>
<h3 ><a href="<?php echo get_permalink( $rpost->ID ); ?>" target="_self"><?php echo get_title($rpost->ID); ?></a></h3>
<div><?php echo get_post_meta($rpost->ID, 'teaser', true); ?></a></div>
<a href="<?php echo get_permalink( $rpost->ID ); ?>" target="_self">Learn More Now »</a>
</div>
<?php
endforeach;
}
?>
This is one example of how you can utilize these tags. You could use them to determine if ads should be shown or which ones, changing layouts, or any number of ways.
Resources
get_posts, get_post_meta, get_the_category, is_page, is_single, is_singular, Tips & Tricks, Wordpress
In addition to using the wp_get_archives function to display archives, WordPress provides a multitude of other functions that you can use to suit your navigational needs. Two similar functions that come to mind are get_posts and get_pages. The get_posts function returns all the posts that match your criteria while get_pages returns pages.
To illustrate how these 2 functions work lets do a couple basic examples.
Example 1: Showing a Recent Posts links limited to a category
For this example lets assume we have a movie review website that posts reviews as well as news and updates on upcoming releases. In this site we have decided we want to display the user with an archive containing the 5 most recent movie reviews we published. To accomplish this task we would use the get_posts page with the category__in and number_posts parameters. We can specify in the category__in parameter the Movie Review category and set the number of posts to 5 in number_posts. The code would look similar to:
<h2 class="recentposts"><?php _e('Recent Reviews'); ?></h2>
<ul class="nav">
<?php
$reviews = get_posts( array('category__in' => array(60), 'numberposts' => 5 ) );
foreach($reviews as $review): ?>
<li><a title="<? echo $review->post_title; ?>" href="<?php echo get_permalink( $review->ID ); ?>"><? echo $review->post_title; ?></a></li>
<?php endforeach; ?>
</ul>
Example 2: Segregating your pages into a main nav and footer nav
With a website you typically have multiple navigation elements the most common being a main navigation on the left or top of your site and a footer (utility) nav on the bottom of the site. To manage the content that appears in these navs you can use the get_pages function to include/exclude content. Lets assume our site has 3 pages we want to display in the footer but not the main nav then we could exclude them from the main nav by:
<div id="navbar">
<ul>
<?php
$pages = get_pages('exclude=5,19,20');
foreach ($pages as $pageInstance) {
$link = '<li><a href="'.get_page_link($pageInstance->ID).'" target="_self">';
$link .= $pageInstance->post_title;
$link .= '</a></li>';
echo $link;
}
?>
</ul>
</div>
And include them in the footer nav by:
<?php
$pages = get_pages('include=5,19,20');
foreach ($pages as $pageInstance) {
?>
|
<?php
$link = '<a href="'.get_page_link($pageInstance->ID).'" target="_self">';
$link .= $pageInstance->post_title;
$link .= '</a>';
echo $link;
}
?>
These are two basic examples of what you can do with the get_pages and get_posts functions. To see a full listing of the options you can pass to them check out the WordPress Codex.
Resources
custom archive, custom navigation, get_pages, get_posts, Tips & Tricks, Wordpress