Oct 27, 2011

WordPress Theme Hacks

WordPress was originally created as a weblog or blog platform. But now WordPress has grown so powerful that you can use it to create any type of website and use it as a Content Management System (CMS). In this article, I'm going to share some of my WordPress tricks with you on how to make a better WordPress theme. I'm not a programmer nor developer, so I will focus more on the frontend development. Oh yeah, I forgot to mention that WordPress has made it so easy that even a non-programmer (designer like me) can build a wonderful website. My WordPress sites included: N.Design Studio, Best Web Gallery, Web Designer Wall, and some free WordPress Themes.

WordPress Conditional Tags 

Conditional Tags are very useful when creating a dynamic WordPress theme. It allows you to control what content is displayed and how that content is displayed. Here are couple sample uses of Conditional Tags:

 

Dynamic Highlight Menu


Here is what I used to create a dynamic highlight menu on Best Web Gallery. In the first list item, if it is Home or Category or Archive or Search or Single, add class="current" to the <li> tag, which will highlight the "Gallery" button. Second item, if it is page with Page Slug "about", add class="current".
<ul id="nav">
<li<?php if ( is_home() || is_category() || is_archive() || is_search() || is_single() || is_date() ) { echo ' class="current"'; } ?>><a href="#">Gallery</a></li>
<li<?php if ( is_page('about') ) { echo ' class="current"'; } ?>><a href="#">About</a></li>
<li<?php if ( is_page('submit') ) { echo ' class="current"'; } ?>><a href="#">Submit</a></li>
</ul>


Dynamic Title tag


Again, I use Conditational Tags to output dynamic <title> tag in the header.php.

<title>
<?php
if (is_home()) {
echo bloginfo('name');
} elseif (is_404()) {
echo '404 Not Found';
} elseif (is_category()) {
echo 'Category:'; wp_title('');
} elseif (is_search()) {
echo 'Search Results';
} elseif ( is_day() || is_month() || is_year() ) {
echo 'Archives:'; wp_title('');
} else {
echo wp_title('');
}
?>
</title>


Dynamic Content


If you want to include a file that will only appear on the frontpage, here is the code:
<?php if ( is_home() ) { include ('file.php'); } ?>


Feature post highlighting


Let's say categoryID 2 is your Feature category and you want to add a CSS class to highlight all posts that are in Feature, you can use the following snippet in The Loop.
<?php if ( in_category('2') ) { echo ('class="feature"'); } ?>


Unique Single template


Suppose you want to use different Single template to display individual post in certain category. You can use the in_category to check what category is the post stored in and then use different Single template. In your default single.php, enter the code below. If the post is in category 1, use single1.php, elseif in category 2, use single2.php, otherwise use single_other.php.
><?php
$post = $wp_query- >post;

if ( in_category('1') ) {
include(TEMPLATEPATH . '/single1.php');

} elseif ( in_category('2') ) {
include(TEMPLATEPATH . '/single2.php');

} else {
include(TEMPLATEPATH . '/single_other.php');

}
? >


Unique Category template


Suppose you want to use different Category template to display specific category. Simply save your Category template as category-2.php (note: add "-" and the categoryID number to the file name). So, category-2.php will be used to display categoryID 2, category-3.php will be used for categoryID 3, and so on.

Display Google Ad after the first post


A lot of people have asked me for this. How to display a Google ad after the first post? It is very simple. You just need to add a variable ($loopcounter) in The Loop. If the $loopcounter is less than or equal to 1, then include google-ad.php code.
<?php if (have_posts()) : ?>

<?php while (have_posts()) : the_post(); $loopcounter++; ?>

// the loop stuffs

<?php if ($loopcounter <= 1) { include (TEMPLATEPATH . '/ad.php'); } ?>

<?php endwhile; ?>

<?php else : ?>

<?php endif; ?>


Query Posts


You can use query_posts to control which posts to show up in The Loop. It allows you to control what content to display, where to display, and how to display it. You can query or exclude specific categories, so you get full control of it. Here I will show you how to use query_posts to display a list of Latest Posts, Feature Posts, and how to exclude specific category.

Display Latest Posts


The following code will output the 5 latest posts in a list:
<?php query_posts('showposts=5'); ?>

<ul>
<?php while (have_posts()) : the_post(); ?>
<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
<?php endwhile;?>
</ul>


Display Feature Posts


Let's say categoryID 2 is your Feature category and you want to display 5 Feature posts in the sidebar, put this in your sidebar.php:
<?php query_posts('cat=2&showposts=5'); ?>

<ul>
<?php while (have_posts()) : the_post(); ?>
<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
<?php endwhile;?>
</ul>


Exclude specific category


You can also use query_posts to exclude specific category being displayed. The following code will exclude all posts in categoryID 2 (note: there is a minus sign before the ID number):
<?php query_posts('cat=-2'); ?>

<?php while (have_posts()) : the_post(); ?>
//the loop here
<?php endwhile;?>


Tips: you can overwrite the posts per page setting by using posts_per_page parameter (ie. <?php query_posts('posts_per_page=6'); ?>)

Custom Fields


Custom field is one the most powerful WordPress features. It allows you to attach extra data or text to the post along with the content and excerpt. With Custom Fields, you can literally trun a WordPress into any web portal CMS. On Web Designer Wall, I use Custom Field to display the article image and link it to the post.

First add the Custom Field in the post.


1 comments:

The Resource Hub said...

Attractive!! This is one of the best things I have read so far on theme hack. I am doing WordPress Website Development and sometimes I am also wondering about this topic and suddenly I came across to your Blog. Really a nice stuff. Please keep writing and sharing on. Thank You.

Post a Comment