Useful WordPress Tricks

June 17, 2008 · Print This Article

As you already know WordPress is one of the most popular blog platforms and it has grown so powerful that you can use it also use as Content Management System (CMS).

Grab Category name

$category = get_the_category();
echo $category[0]->cat_name;

So this grabs a single category name (it’s not printing it just keeps it for now).

Template Page

Template Name: Free Themes

<?php get_header(); ?>
//the loop
<?php get_footer(); ?>

You should be familiar with this it makes a custom page template which you have to assign to some page after creating it.

Featured post

With query_posts()

<?php query_posts('cat=3&showposts=1'); ?>
<?php while (have_posts()) : the_post(); ?>
<?php the_excerpt('Read the rest of this entry »'); ?>
<?php endwhile; ?>

query_posts() is very useful and you can use it where ever you want, display latest posts (<?query_posts(‘showposts=10’);?>) for example or client testimonials, exclude a specific category (<?php query_posts(‘cat=-3’); ?>) etc…

With new WP_Query()

<?php $recent = new WP_Query("page_id=2&showposts=1"); while($recent->have_posts()) : $recent->the_post();?>
<?php the_excerpt('Read the rest of this entry »'); ?>
<?php endwhile; ?>

You’ll notice these two are very similar so what ever you feel like using, i personally use the first one links and the second one for content.

The php solution

<?php $i=1; ?>

<?php while (have_posts()) : the_post(); ?>

<?php if ($i == 1) { ?>
<div id="post-<?php the_ID(); ?>" class="featured">
featured content

<?php } else { ?>
<div id="post-<?php the_ID(); ?>">
rest of content

<!-- post -->
<?php } ?>

<?php $i++; ?>
<?php endwhile; ?>

I think it’s pretty much clear, it changed first post into a different div style (you can use here what ever you want of course). The only thing i don’t like about this way is that your main loop in this case has to be in seperate div, you know you can’t have anything between featured post and rest of posts (for example sidebar etc…) . So becarefull how you will code it into html first.

Include a specific file

<?php include (TEMPLATEPATH . '/sidebar2.php'); ?>

If you need to include some other file than <?php get_sidebar(); ?> for example different sidebar or footer file etc…

Conditional Tags

if ( is_front_page() ) { include (TEMPLATEPATH . '/home1.php'); }
else { include (TEMPLATEPATH . '/rest.php'); }

Don’t think it needs some explanation, this also could be used for featured content/post and here are some other Conditional Tags:

is_home(), is_category(), is_archive(), is_search(), is_single(), is_date(), is_404(), etc…

Reverse post order

In your index.php file, look for this bit of code:

<?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>

Right before that line, add this code:

<?php query_posts($query_string . “&order=ASC”) ?>

If you are the old-fashion guy and like the old ones on the top


Bad code used in title tags or search templates:
<?php echo $s; ?>

as it allows malicious code injection.

You should use this one
<?php echo wp_specialchars($s, 1); ?>

Reverse comment order

<?php $comments = array_reverse($comments, true); ?>
<?php foreach ($comments as $comment) : ?>
content here
<?php endforeach; ?>

Your choice for site templates and wordpress themes


Got something to say?

You must be logged in to post a comment.