StylizedWeb is a blog about web trends and tutorials, KnowHow community forum now maintained by Ross Johnson who also runs a web design company and design blog.

Categories Archive

Popular Posts
Advertisement
Premium WordPress Themes, Theme Galaxy Basecamp
Najbolje ponuda IT poslova u Srbiji na itposlovi.info Advertise on Stylized Web

Useful WordPress Tricks

add to Save to Delicious Save to Stumble Save to Digg

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; ?>
 

What do you think?





125 Responses so far

By haberler
on December 3, 2008

Thanks for sharing these solutions, and congrats for your blog design

By neyazsak
on November 5, 2008

i search this thing for my website thnks

By NimeBriseet
on October 31, 2008

Админчег :) У меня к тебе небольшое предложение, хоть и не по теме блога ;) Напиши пожалуйста свой обзор передачи Гордон Кихот. Особенно прошлый выпуск, про Шансон.

Спасибо :) Удачи дружище

By plc eğitimi
on September 15, 2008

Thanks Dejan.

By Kudungga
on September 11, 2008

thanks a lot for sharing. Good luck for you

By BilgiDeposu
on August 31, 2008

cool .. thank you for post.

By Şahin
on August 23, 2008

Thank you.

By Volkan
on August 12, 2008

Thank You. great trick…

By lig tv
on July 20, 2008

Really nice set of tricks for WP. Thx!

By yemek tarifleri
on July 20, 2008

I would like to know, would htmlentities() instead of wp_specialchars() work fine too?

By performans ödevleri
on July 20, 2008

Thanks for sharing these solutions, and congrats for your blog design.

By Eric Larson
on July 16, 2008

Great article?
You mentioned you using query_posts() for grabing links..
How do you grab a specific link category and display those links in the footer?

By CialisKt
on July 16, 2008

Nice Article

By mirc
on June 26, 2008

Thanks you very much

By Dejan Cancarevic
on June 21, 2008

Cezar,
please give us a more detailed explanation,tell us what part of this article are you referring to and past your code here

By cezar
on June 21, 2008

great trick,

i have one question though. why cant i get the tags to show.
i added in the loop but they dont show. are they supposed not to work? or am i doing something wrong?

thanks,
C

By Dejan Cancarevic
on June 20, 2008

you are welcome ;)

By Max
on June 20, 2008

Dejan Cancarevic “Max,
Try this http://stylizedweb.com/2008/03/01/syntax-highlighter/

Thanks for the link!

By Dejan Cancarevic
on June 19, 2008
By Harley Alexander
on June 19, 2008

You, my friend, are a champion! I know this might be pushing the envelope, but WP_Query() is restricted to posts, or can I get a list of recent comments too in a similar way?

By Dejan Cancarevic
on June 19, 2008

Harley,
Sure it is, use WP_Query() as i says in text, so try this ;)

<?php $recent = new WP_Query(”showposts=5″); while($recent->have_posts()) : $recent->the_post();?>
<li><a href=”<?php the_permalink() ? rel=”nofollow”>” rel=”bookmark” title=”Permanent Link to <?php the_title(); ?>”><?php the_title(); ?></a></li>
<?php endwhile; ?>

By Harley Alexander
on June 19, 2008

Hey, I’m just having a bit of trouble with the query_posts():

I want to know if it’s possible to have such content needed (I need a list of post titles) BEFORE the main loop? I have a section in my header that I wanted to include recent posts in (without using a plugin), however when I use the exampled ‘query_posts() code, it shows what I want up top in the header, but then the content in the main loop is whatever was specified in the query_posts(), which for example was just the 1 post from a certain category.

Is it possible to ‘end’ the use of the query_posts() for the header only? When I pasted the code AFTER my main loop, it worked! I’m confused as to why this is so, maybe you could shed some light on it for me!

Thanks a lot, and fantastic post by the way. Learnt some cool stuff!

~Harley

By Dejan Cancarevic
on June 19, 2008
By Max
on June 19, 2008

Thanks for the post! Helped me a lot.
Just a side-question, how did you get the code displayed? What plugin? Thanks

Max

By Durkin
on June 17, 2008

really cool tips thanks! :)

By Dejan Cancarevic
on June 17, 2008

Karen,
go ahead and post your question to
http://stylizedweb.com/knowhow

By Karen Collins
on June 17, 2008

Dejan, nice job! I’m new to Wordpress (& am not a template designer) . .. am wondering how I can change the link text color from white to black? The white just doesn’t show up very well . . . I’m using the Sweet Sunset Wordpress Theme. Thanks for any help you can give!

By Zhe
on June 16, 2008

Very useful tips, thanks, Dejan!

By Tom
on June 16, 2008

Thanks for sharing these solutions, and congrats for your blog design.

By Dejan Cancarevic
on June 16, 2008

All,
Thank you for the good words, i really appreciate it! ;)

By rapmatix
on June 15, 2008

Thanks Dejan ::)

By rap dinle
on June 15, 2008

Woot! Congrats!

By willy
on June 15, 2008

i come from chinese, and i like your design. i have use it in my second blog, thanks, it look great.

By Patrick Sweeney
on June 15, 2008

Awesome site!

This are all some pretty useful tips. I am going to add a link to this post and your site on my blog.

By Jenny
on June 15, 2008

Cool. I’m gonna use these!

By tasarhane
on June 15, 2008

thanks for this good article..

By Erik Pettersson
on June 15, 2008

Really nice set of tricks for WP. Thx!

By Bingu
on June 14, 2008

Thx, Dejan

By Referáty
on June 14, 2008

These tips are really great.

By Dejan Cancarevic
on June 14, 2008

Bingu,
I’ve updated the post with it

By Bingu
on June 14, 2008

How to reverse comment order?

By Darran
on June 14, 2008

I would like to know, would htmlentities() instead of wp_specialchars() work fine too?

By ChaosKaizer
on June 14, 2008

just wanna touch the include specific files tips for sidebar
if you have additional sidebar like for single or archive you can do it like the following (WP2.5.x)

get_sidebar($name); // arg $name is sidebar-”name”.php

the function will look for sidebar-single.php (inside theme dir) and load it if exists otherwise it will load the default sidebar.php.

you can include section base sidebar base on wordpress hierarchical order. below is just a simple example (need to tweak it base on which section came first thought)

if (is_single()){
get_sidebar(’single’); // include /theme/sidebar-single.php
} elseif (is_archive()){
get_sidebar(’archive’); // include /theme/sidebar-archive.php
} else {
get_sidebar();
}

By Aldo
on June 14, 2008

Hey, WP cut off my code!
Here’s the code: http://paste.ubuntu.com/20100/

By Dejan Cancarevic
on June 14, 2008

heh yea sorry i hate when WordPress does this.. ;)

By Aldo
on June 14, 2008

Thanks Dejan. ;)

Only a correction:

<a class="continuelink" href="" rel="nofollow" title="Continue reading ">… continue reading

By Dejan Cancarevic
on June 14, 2008

johnbillion,
thanks i didn’t know about that one ;)

Teli Adlam ,
I’ve been using is_home for a while now so thanks for the heads up!

By Ashfame
on June 14, 2008

Thanks for the tricks. Stumbled. Will come handy anytime.

By autworld
on June 14, 2008

great infos about wp!

By Teli Adlam
on June 14, 2008

Nice write up. Picked up a few ideas for use in future themes, so thanks for taking the time to compile it.

You may want to add a note about is_home as it’s been slightly tweaked for version 2.1.x and up. (Basically, is_page(’home’) seems to be the preferred conditional tag when working with newer versions of WordPress.)

By johnbillion
on June 14, 2008

Dejan,

Instead of wp_specialchars() I use attribute_escape(). There is no difference to the output, but attribute_escape() is preferred as it passes the output through the attribute_escape filter first.

By Dejan Cancarevic
on June 14, 2008

just add manually
<a class=”continuelink” href=”<?php the_permalink(); ? rel=”nofollow”>” title=”Continue reading <?php the_title(); ?>”>… continue reading</a>

By Aldo
on June 14, 2008

Pardon: inside the parenthesis in the_excerpt() there is ‘Read the rest of this entry’

By Aldo
on June 14, 2008

Here’s my code in my sidebar: http://paste.ubuntu.com/20006/

By Iva
on June 13, 2008

Hvala!

By Dejan Cancarevic
on June 13, 2008

Steven,
Thanks buddy ;)

Aldo,
Go ahead and past your code here and we’ll see what is it

By Steven Snell
on June 13, 2008

Nice resource for all WP theme designers.

By david
on June 13, 2008

I’m ashamed to admit I’ve never heard the last one before. It’s especially troubling because it’s the one where my ignorance could have negative consequences.

By Aldo
on June 13, 2008

Nice tricks! ;)

I’d like to know why — using ‘The featured post’ — the line ‘Read the rest of this entry’ is not displayed in my sidebar.

By Lazy
on June 13, 2008

Hi,

really good article :)

have a nice day

Chris

By Dejan Cancarevic
on June 13, 2008

Andrew,
heh i’m glad you like it ;) If you have some PW issues or questions go ahead and post them b/c that’s how i’ve written this article - people asked questions, so i’ll be happy to help..

Ivan,
I think so too ;)

By Ivan Nikolic
on June 13, 2008

Been using some of them for long time now, good to see them in one place :)

By Andrew
on June 13, 2008

Excellent! I can’t tell you how many PHP 101 sites I have had to sift through to get this information (I’m much more a designer than programmer). -If only I would have found this post earlier!

By Ivan
on June 13, 2008

heh i’m not using any of them, shame on me ;)

By Christina
on June 13, 2008

really useful info, thanks!

  1. Nov 20, 2008: Nove trucchi per WordPress — Studio404 Web Agency
  2. Nov 15, 2008: Feed Subscriptions, Stats and Management « Feet up, eyes closed, head back
  3. Aug 29, 2008: Learning MooTools: 20 MooTools Tutorials and Examples « Jonsunhee’s Weblog
  4. Aug 26, 2008: Readers Pick: 12 Excellent Websites to Follow if You’re into Web Design | Asktechman.com -Your Guide to best Internet Resources
  5. Aug 17, 2008: Readers Pick: 12 Excellent Websites to Follow if You’re into Web Design | [w3b]ndesign
  6. Aug 5, 2008: 25 WordPress Tricks and Plugins to Make Your Blog Stand Out | Oragle
  7. Jul 24, 2008: Amazing WordPress hacks part 1 | Mexzhouse Design Studio
  8. Jul 24, 2008: 博客士 » Blog Archive » wordpress几个有用的模板函数
  9. Jul 14, 2008:   Table Of Contents Of Wordpress Tutorials, Helps, Tips and Tricks by aComment.net
  10. Jul 1, 2008: Best of June 2008 : : Life as a Web Designer
  11. Jun 30, 2008: Pages tagged "useful"
  12. Jun 29, 2008: blog.rotracker.net » Blog Archive » Useful WordPress Tricks (for the Theme Designer)
  13. Jun 25, 2008: Amaizing WordPress hacks part 1 | StylizedWeb.com
  14. Jun 25, 2008: Tips de desarrollo para Wordpress | frogx.three
  15. Jun 22, 2008: Useful WordPress Tricks : Freelance Folder
  16. Jun 22, 2008: Trucos de wordpress | OLDSKULL WEBSITE!
  17. Jun 21, 2008: Utili Tricks per wordpress su stylizedweb.com | tutorialweb blog
  18. Jun 20, 2008: Grab data from WordPress database | StylizedWeb.com
  19. Jun 19, 2008: Some handy articles and links I don’t want to lose… @ room34.com
  20. Jun 19, 2008: links for 2008-06-19 — SOJo: Student of Online Journalism by Megan Taylor
  21. Jun 18, 2008: Design Spotlight: StylizedWeb | Webmaster-Source
  22. Jun 18, 2008: Useful WordPress Tricks | StylizedWeb.com — hepp.se
  23. Jun 18, 2008: Doua alegeri “inspirate”? « Daily life activities using ICT
  24. Jun 18, 2008: Make Your WordPress Blog Go From Blah to Awesome at The Daily Thing
  25. Jun 18, 2008: Useful WordPress Tricks » Techtites
  26. Jun 17, 2008: Useful WordPress Tricks | Daily, Tips and A Whole lot more
  27. Jun 16, 2008: Trucos para personalizar tu theme de WordPress | Bitperbit
  28. Jun 16, 2008: Trucos de Wordpress muy útiles
  29. Jun 16, 2008: PerformSec Blog » Blog Archivo » Trucos útiles para wordpress
  30. Jun 16, 2008: Useful WordPress Tricks - 漫步
  31. Jun 16, 2008: Script-Montag: Nützliches im Web. - im Designpicks Blog
  32. Jun 15, 2008: Weblog Tools Collection: Useful WordPress Tricks (for the Theme Designer) | JERSEY-BARKER
  33. Jun 15, 2008: Awesome Small WordPress Tricks | blackleafmedia
  34. Jun 15, 2008: links for 2008-06-15 | JeremiahTolbert.com
  35. Jun 15, 2008: Wp Wordpress » Blog Archive » WordPress Talk - June 15, 2008
  36. Jun 15, 2008: 3 link-uri de retinut (pentru bloggeri si web designeri) | Idei Geniale
  37. Jun 15, 2008: WordPress tricks | Blogging Tips
  38. Jun 15, 2008: WordPress Talk - June 15, 2008
  39. Jun 15, 2008: Links for 15-06-2008 | Velcro City Tourist Board
  40. Jun 15, 2008: links for 2008-06-15 at DeStructUred Blog
  41. Jun 15, 2008: Reader Pick: 12 Excellent Websites to Follow if You're into Web Design - Six Revisions
  42. Jun 15, 2008: links for 2008-06-14 « toonz
  43. Jun 14, 2008: links for 2008-06-14 « RabiFoot at wordpress.com
  44. Jun 14, 2008: 两枚WordPress模板小技巧
  45. Jun 14, 2008: Internetpret voor 14-06-2008 | Wat mij opvalt
  46. Jun 14, 2008: » Useful WordPress Tricks Webcreatives
  47. Jun 14, 2008: Utili Tricks per wordpress su stylizedweb.com : technorati.it
  48. Jun 14, 2008: diarioTHC | Trucos útiles para wordpress
  49. Jun 14, 2008: nerdd.net | news and opinion
  50. Jun 14, 2008: WordPress Tips: 一些实用的WordPress模板技巧 at WordPress Today
  51. Jun 14, 2008: Skylog » Blog Archive » links for 2008-06-14
  52. Jun 14, 2008: Leonaut.com
  53. Jun 14, 2008: WPforSale
  54. Jun 14, 2008: Featured posts senza alcun plugin » Ubuntu block notes
  55. Jun 13, 2008: » Useful WordPress Tricks « Аз, пиратът
  56. Jun 13, 2008: Useful WordPress Tricks (for the Theme Designer) | BlogBroker24-7
  57. Jun 13, 2008: Wp Wordpress » Blog Archive » Useful WordPress Tricks (for the Theme Designer)
  58. Jun 13, 2008: Weblog Tools Collection » Blog Archive » Useful WordPress Tricks (for the Theme Designer)
  59. Jun 13, 2008: Useful WordPress Tricks
  60. Jun 13, 2008: f list