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 &raquo;</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 &raquo;</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

, , , , , , ,

When writing a post to your WordPress powered blog or website have you ever wished you could split it up into multiple pages? The good news is that you can WordPress comes by default with the ability to create paginated posts by use of the Page-Link tag. This tag notifies WordPress that the following content should be on different page then the proceeding content.

The tag is used by simply inserting the text <!–nextpage–> into your post. There used to be a button to make this task easier on the user but it was removed to clean up the tools bar. You may find that this tag is not working for you in your site, the reason for this is that the theme you are using failed to implement the Page-Link function into their templates to display the pages. You can correct this by calling the wp_link_pages function in your single post template.

<?php wp_link_pages(); ?>

You can fully customize the page link by the usage of various parameters including before, after, next_or_number, and other self explanatory options. For a full list of options check out the Styling Page-Links page of the WordPress Codex.

, , , , , ,

Very often when developing a WordPress powered blog or website you may find that you need to add custom features. One of the most common feature requests that I have come across is to add an image to the excerpts that get displayed on the main posts/articles page. Some people will try to utilize the excerpt to have the image display but there is a much easier way, the Custom Fields feature of WordPress.

The Custom Fields feature is used to add properties to your posts that you can then use in your template files. The default Custom Fields interface provided by WordPress is pretty basic in that you can enter name value pairs where you can put anything you want as a value.
WordPress Custom Fields Example

To make using Custom Fields easier I would suggest utilizing a plugin such as Custom Field Template. This plugin allows you to create templates with defined fields for you to easily create your custom properties to your posts. In addition it enables you to have drop downs, text fields, text areas, and even the media hook in to insert images from your media content or upload them from your computer.
WordPress Custom Field Template

With the introductions to Custom Fields out of the way lets get right into an example on how to enhance your blog using them.

Example : Displaying an image with your blog excerpts

The simplest way to display an image in your blog excerpts is to create a custom field that will hold the image html that you want to display. If using the Custom Field Templateplugin then set the media to true so you can use the built in WordPress media tool to choose and modify your image. Lets assume for our purposes we named this property “img_thumb”. The first step is to identify the template(s) you will need to modify, in this example we will be modifying the main template responsible for showing the blog posts. Lets assume that our excerpts are displayed similar to the following sample code:

<div class="content">
    <h3>
        <a class="link" href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a>
    </h3>
    <div class="entry"><?php the_excerpt(); ?></div>
</div>

In this basic sample we are displaying the post title followed by excerpt. If we wanted to enhance this code to show the thumbnail image then we would have to first obtain the custom fields for the posts using the get_post_custom method, that returns an associated array of all the custom fields for the post.

<?php $custom_fields = get_post_custom(); ?>

Now that we have obtained the custom fields we can use them in our template. Lets modify the template so that we insert the image into our post prior to displaying the excerpt.

<?php $custom_fields = get_post_custom(); ?>
<div class="content">
    <h3>
        <a class="link" href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a>
    </h3>
    <div class="entry"><?php echo $custom_fields['img_thumb'][0]; ?><?php the_excerpt(); ?></div>
</div>

It is important to note the use of the [0] 0 index on the custom field property. As Custom Fields allow for multiple values for the same name it is important to use the index to specify the use of the value. This code above will now display the image in your blog posts. However you may have it so that you don’t have a image for each post, in that case you will want to check if the property exists before using it. If wanted you could even put in a default image to be displayed if a value isn’t entered.

<?php $custom_fields = get_post_custom(); ?>
<div class="content">
    <h3>
        <a class="link" href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a>
    </h3>
    <div class="entry">
        <?php if( $custom_fields['img_thumb'][0] != '' ) {
            echo $custom_fields['img_thumb'][0]; 
        } else { ?>
            <img src="http://www.example.com/wp-content/uploads/2010/02/test.jpg" alt="Alternate Text" title="Image Title" width="114" height="180" />
        <?php } ?>
        <?php the_excerpt(); ?>
    </div>
</div>

You have now enhanced your blog to display an image with your post excerpts. There are an unlimited number of ways to use custom fields in your blog from displaying special content and different layouts. Take the time to determine common content in your posts and then use Custom Fields to save and display this information in the format you need.

Resources

, , , , ,

As a certain project at work evolves I find that more and more of my time is spent on optimizing the T-SQL queries that are utilized. Recently I have come across the issue that an index was not getting utilized in a sql query when I was using the UPPER or LOWER function to do an ignore case comparison. As it turns out that using a function on a column causes a table scan and will not use the index ever.

To resolve this issue we fell back on regular expressions as T-SQL does not support the ILIKE function. What this means is that we can use regular expression sets for each character in our String. To illustrate lets look at the following basic SQL string.

SELECT * FROM USERS WHERE UPPER(LAST_NAME) LIKE "DENTON%"

If our table had an index on the LAST_NAME column then it would be ignored. To use our index we would have to rewrite the query to be:

SELECT * FROM USERS WHERE LAST_NAME LIKE "[dD][eE][nN][tT][oO][nN]%"

This may seem odd, but running this second query will utilize the column index. Another item to note is that you can only use the regular expressions in LIKE clauses and not = clauses.

Resources

, , , , , ,

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) {
?>
        &nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;
<?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

, , , , ,