StylizedWeb

Subscribe to our updates:

A Design, WordPress and Tutorials Blog.

Dedicated to helping you learn the art and science of the web.

Useful 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).

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
  1.  
  2. <?php
  3. $category = get_the_category();
  4. echo $category[0]->cat_name;
  5. ?>
  6.  

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
  1.  
  2. <?php
  3. /*
  4. Template Name: Free Themes
  5. */
  6. ?>
  7.  
  8. <?php get_header(); ?>
  9. //the loop
  10. <?php get_footer(); ?>
  11.  

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()

  1.  
  2. <?php query_posts('cat=3&showposts=1'); ?>
  3. <?php while (have_posts()) : the_post(); ?>
  4. <?php the_excerpt('Read the rest of this entry &raquo;'); ?>
  5. <?php endwhile; ?>
  6.  

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()

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

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

  1.  
  2. <?php $i=1; ?>
  3.  
  4. <?php while (have_posts()) : the_post(); ?>
  5.  
  6. <?php if ($i == 1) { ?>
  7. <div id="post-<?php the_ID(); ?>" class="featured">
  8. featured content
  9. </div>
  10.  
  11. <?php } else { ?>
  12.  
  13. <div id="post-<?php the_ID(); ?>">
  14. rest of content
  15. </div><!-- post -->
  16. <?php } ?>
  17.  
  18. <?php $i++; ?>
  19. <?php endwhile; ?>
  20.  
  21.  

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
  1.  
  2. <?php include (TEMPLATEPATH . '/sidebar2.php'); ?>
  3.  

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

Conditional Tags
  1.  
  2. <?php
  3. if ( is_front_page() ) { include (TEMPLATEPATH . '/home1.php'); }
  4. else { include (TEMPLATEPATH . '/rest.php'); }
  5. ?>
  6.  

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

  1.  
  2. is_home(), is_category(), is_archive(), is_search(), is_single(), is_date(), is_404(), etc...
  3.  
Reverse post order
  1.  
  2. In your index.php file, look for this bit of code:
  3.  
  4. <?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
  5.  
  6. Right before that line, add this code:
  7.  
  8. <?php query_posts($query_string . “&order=ASC”) ?>
  9.  

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

wp_specialchars()
  1.  
  2. Bad code used in title tags or search templates:
  3. <?php echo $s; ?>
  4.  
  5. as it allows malicious code injection.
  6.  
  7. You should use this one
  8. <?php echo wp_specialchars($s, 1); ?>
  9.  
Reverse comment order
  1.  
  2. <?php $comments = array_reverse($comments, true); ?>
  3. <?php foreach ($comments as $comment) : ?>
  4. content here
  5. <?php endforeach; ?>
  6.  
Exclude first posts/post
  1.  
  2. <?php $posts=query_posts($query_string . 'posts_per_page=10&offset=2'); while (have_posts()) : the_post(); ?>
  3. this will exclude first two posts
  4. <?php endwhile; ?>
  5.  

Leave a comment on Stylized Web Have some feedback? Leave a comment

423 Comments So Far

  1. am I would like to appreciate your work and would like to tell to my
    kolega

  2. Hi,

    I liked it & I’m using it.

    One thing I just wanna know, the “read more” option showing a bizzare link?

    And the blackquote isnt also working properly.

    Would u plzz help?

  3. Those who want to enter the world healthy and permanent form, which is a product of choice for African Mango, now in Turkey. Because people prefer to use a 100% herbal product that helps people to enter the African Mango form a single product.

  4. Very nice post is also very lively and attractive you look .every one who wanted to congratulate you You look super

  5. Good work,hope your blog be better!I just want to make a blog like this!for your nice sharing!Just keep on.

  6. Hey… just found this post after lonikog for a little while thanks. Wanted to add i was using a plugin called redirection to handle when i rename posts and also to help remove the category base from the permalink url.The plugin was causing a 404 when going to page 2. I had to disable one of it’s redirect matches. I haven’t ascertained whether it will throw off the rest of my site… we shall see.Thanks for the help, led me on the right path!

  7. fT7Q4a qerqjeximtsi

  8. thank you, good work master. ;)

  9. mNNmt5 piecqapkaxni

  10. Thanks,

  11. Good work,hope your blog be better!I just want to make a blog like this!for your nice sharing!Just keep on.

  12. thank nice blog..

  13. guzel bir blog tesekkurler

  14. Elinize sağlık çok iyi olmuş.

  15. Orourke Aiken quarterly. DJ FX Trader covers FX markets from every angle.ARM lenders use one of these as the base interest rate and then add a margin (which represents their income from the loan) to come up with the rate you pay. Demand: A franchise that’s been successful in other areas of the coun-try (or even your state or city) won’t necessarily be successful in yours.tends to lead to ‘‘destructive’’ chaos.it has tried to break out on the upside.

  16. This does look like a great theme.

  17. Nice tricks, espcially the last one to exclude the first post.

  18. thanks, your blog be use ful wordpress tricks,I just want to make a blog likes .for your nice sharing.

  19. Great tricks! I try it someones from this and write about results. by the way, is it still work after all WP updates?

  20. But every year (or sometimes every other month), new designs come in and sweep away the not-so-new ones. If you like to keep up with the trends and wants nothing but the best and the latest, then you’d surely want to get your credit cards on the Split Thump Sunglasses. Would you believe it ‘ this pair of sunglasses features removable earbuds and disguised controls for the MP3 player installed on the glasses itself! Now you can jog along and enjoy the music without the annoyance of wires around you. Although, this might not be big news, since its predecessor, oakley sunglasses outlet, have such a feature as well. But unlike the predecessor, the Split Thump has a softer and less severe style and can also be worn by either sex. cheap oakleys sale http://pinterest.com/oakleyscheap/cheapoakleys/

  21. Admission and jobs portal is one of the best Education Portal providing. Colleges , MBBS FROM HAMDARD INSTITUTE OF MEDICAL SCIENCES & RESEARCHUniversities, Institute, Schools and other details related to Education. We are

  22. elinize sağlık yararlı bilgiler…thanks

  23. If you desire to take much from this article then you
    have to apply such techniques to your won weblog.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>