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
 
<?php
$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
 
<?php
/*
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 &raquo;'); ?>
   <?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 &raquo;'); ?>
<?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
</div>
 
<?php } else { ?>   
<div id="post-<?php the_ID(); ?>">
      rest of content
</div>
 
<!-- 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
 
<?php
 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 ;)

wp_specialchars()
 
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; ?>
 

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

417 Comments So Far

  1. Pingback: Reader Pick: 12 Excellent Websites to Follow if You're into Web Design | Graphic Techniques

  2. Thanks for sharing. A real lot of useful info here!

    These are all great comments here. Very cool article.

  3. And now you encounter this challenge. In reality, it is completely probable to resolve this dilemma. Nevertheless, you must take some time to do so. And here are

  4. Buying Moncler Jackets is a wonderful solution to against the extreme cold in winter. Founded by French in 1952, the course of the company has seen several twists and turns which have led this company all this distance to where it is now. Cheap Moncler Coats The company got its name from a French town called Moncler Sweater De Clermont located near Grenoble. Moncler Vests The fashion wear produced by this particular company has seen several decades since it started producing only Moncler Shoes for Women, way back in the 1950s after its low profile launch.

    The commodities under the Cheap Moncler Coats are the representative of the luxury since it has got such great fame in the world. The Moncler Sweater in fact is not very hard for people to get some pieces of it, the goods are made of the better down, which can hold the wearer’s warmth and stop them from the severe winter. Moncler Jackets will no doubt bring the wearer mildness, comfort and also the chic feeling. The styles of Moncler Vests are of great value both for men and Moncler Shoes for Women.

  5. Don’t think it needs some explanation, this also could be used for featured These are all great comments here. Very cool article.

  6. guzel okey guzel okey oynamak istersenız guzelokey adresine bekeleriz….

  7. perfect tips and hints. thank you

  8. The subsequent time I read a blog, I hope that it doesnt disappoint me as much as this one. I imply, I do know it was my choice to learn, but I truly thought youd have something fascinating to say. All I hear is a bunch of whining about something that you can fix for those who werent too busy on the lookout for attention.

  9. goruntulu sohbet

  10. oh my!! i love you so much!!

    It’s really great article. I would like to appreciate your work and would like to tell to my friends.

  11. thanks for admin wonderfull blog and so many information..

  12. The post is very usefull and it contain graet ideas

  13. was a very nice description of the subject

  14. i’ve just started to learn wordpress, have a lot of questions..and this article is so useful!

  15. thanks for useful wordpress tricks..

  16. readly gooooooood

  17. thank you for admins wonderfull blog and so many information ..:!.:! :

  18. that was usefull

  19. i’ve just started to learn wordpress, have a lot of questions..and this article is so useful!

  20. I used to be very pleased to locate this particular internet-site.I want to to be able to many thanks on your own time for this glorious discover!! We without doubt experiencing each tiny little bit of this and I’ve you bookmarked to see new items you blog site publish.

  21. Located you website by using askjeeve I must say I m astounded together with your content!

  22. Located you website by using askjeeve I must say I m astounded together with your content!

  23. Excellent! Thank you very much for the information shared. I already bookmarked your page on Delicious. Regards, my friend.

  24. Great site with very good look and perfect information
    thankss

  25. Thanks admin. Good inquire

  26. hello to everyone who has blog.first I congratulate the blog owner.he is got excellent blog.actually I read all your articles from weary and your writing very attractive waiting to continue thanks

  27. wow amazing i love it ..perfect.

  28. ow harika görünüyor, grafik cant gitmek için beklemek şaşırtıcı görünüyor! thanks you admin elime saglık

  29. ow harika görünüyor, grafik cant gitmek için beklemek şaşırtıcı görünüyor! Hep Ben Ğüldüm Sende ğül Kadar

  30. ow harika görünüyor, grafik cant gitmek için beklemek şaşırtıcı görünüyor! Yar Yar yar Vay Vay

  31. güzel paylaşımlar.

  32. very useful post. thanks admin.

  33. Nice indeed thanks

  34. Great site with very good look and perfect information
    thanks.

  35. chat odası

  36. Thank you,I study more thing with your article

  37. I like it very much, I hope you will soon update your work!thanks for sharing

  38. By Trinity Guinan posted on November 21, 2011 at 5:21 pm

    Why really should we encourage our children to read Bible stories or why must we carry the time for you to study Bible stories to them? Are there not a great number of other interesting and interesting resources accessible today, in a very earth that majors on entertaining men and women of all ages?

  39. I already bookmarked your page on Delicious. Regards, my friend.

  40. I would like to appreciate your work and would like to tell to my friends.

  41. I like this one “Include a specific file”

  42. I really liked this site. I will follow you always. something important to share with. Life is beautiful by sharing. Thank you very much for sharing. çet I already bookmarked your page on Delicious. Regards, my friend.

  43. WP Comment Remix which promises to save you time and effort when running your blog.

  44. look .every one who wanted to congratulate you You look super

  45. I got the information I was looking for a long time this article is very useful. Thanks to the author.

  46. I would like to thnkx for the efforts you have put in writing this blog. I am hoping the same high-grade site post from you in the upcoming as well. Actually your creative writing skills has inspired me to get my own site now. Really the blogging is spreading its wings quickly. Your write up is a great example of it.

  47. I study more thing with your article

  48. it is completely probable to resolve this dilemma.

  49. Good tricks I have bookmarked this page for feature reference

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>