Try Out New Website HTML Table- Creator HTML Table Generator


WordPress Tricks After a short break of writing on WordPress and on the request of my subscribers i decided to make a list of useful WordPress tricks

WordPress Tricks

After a short break of writing on WordPress and on the request of my subscribers i decided to make a list of useful WordPress tricks and you are probably already using some of them, if not going to when making a WordPress theme. As you already know WordPress is one of the most popular blog platforms these days and it has grown so powerful that you can use it also use as Content Management System (CMS).

Useful WordPress Tricks
Useful WordPress Tricks

If you are new to WordPress and blogging itself you should read "Before you start blogging" article and go with some free themes for now, which you can find here in free themes directory.

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) and I've been using it a lot while developing themes

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. I've written a bit of it on how to make a archive page article and you can read more about it here

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. I've used it in my "Tabbed Featured Post" tutorial, click here to see how it looks like.

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). I've used this in my Solemnity free WordPress theme and if you would like to see how it looks click here for demo page. 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...) like here on Stylized Web. 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; ?>

 Exclude first posts/post

<?php $posts=query_posts($query_string . 'posts_per_page=10&offset=2'); while (have_posts()) : the_post(); ?>
this will exclude first two posts
<?php endwhile; ?>

Thank You

Rate this article


Post a Comment

Cookies Consent

This website uses cookies to ensure you get the best experience on our website.

Cookies Policy

We employ the use of cookies. By accessing Lantro UI, you agreed to use cookies in agreement with the Lantro UI's Privacy Policy.

Most interactive websites use cookies to let us retrieve the user’s details for each visit. Cookies are used by our website to enable the functionality of certain areas to make it easier for people visiting our website. Some of our affiliate/advertising partners may also use cookies.