StylizedWeb is a web design trends and tutorials blog, 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; ?>
 

Have some feedback? Leave a comment



What do you think?





200 Responses so far

By www.csidedektiflik.com
on July 10, 2009

Wow! Really good work.Thank You.

By video seyret
on July 8, 2009

thank you administratör

By eğitim haberleri
on July 8, 2009

thanks very googd thank you admins

By Jack
on June 12, 2009

Hi, WordPress 2.8 is come out, do all the tips work?

By oyunoyna
on May 25, 2009

Thanks

By mynet oyun
on May 25, 2009

Thanks you

thanks for code

By Tekkenci Blog
on May 23, 2009

good article thanks :)

By Tekkenci Blog
on May 22, 2009

good idea thanks man.

good work thank you .

By Oyunpark
on May 22, 2009

This for wordpres ?

By Oyunsite.co.cc
on May 22, 2009

thanks for share !

By Araf
on May 22, 2009

great sharing thans !

By niyat
on May 19, 2009

thanks C*

By elektrot
on May 14, 2009

INTERFERENCE FOREIGN TRADE web environment
We offer all products with the best service initiatives, improving the quality of service the products you ihtal web environment provides a detailed way. Category dedicated to the product range with the desired structure, providing convenient access to products from our web site to review the order or request for information from the contact form to reach us

By oyunindiryukle.blogspot.com
on May 14, 2009

Play Free Online Games, sports games, massive multiplayer games, action games, puzzle games, flash games and more, casual games.

By guzelokey
on May 6, 2009

thank

By freshalex H
on April 29, 2009

Very useful, Im thinking about getting wordpress for my website soon

By en güzel oyunlar
on April 24, 2009

thanks for tutorials

By ktunnel
on April 22, 2009

thanks

By xxddxx
on April 18, 2009

heyy..
i copy this enty to my blog…
but dun worry..
i put your link in this entry
:)

By assineewago
on April 17, 2009

nice, really nice!

By Bakimli Kadin
on April 14, 2009

great article thanks

By informatique
on April 13, 2009

Bookmarked ! thank you.

By komik videolar
on April 7, 2009

good article,

thanks a lot

By ahmet
on April 2, 2009
By yemektarifleri
on March 29, 2009

really good article

have a nice day

Chris

By Joe
on March 22, 2009

Great nice

By oyun indir
on March 22, 2009

Great nice

By mp3
on March 21, 2009

Turkisch free mp3 download.

By mp3
on March 21, 2009

thanks

By edebiyat
on March 17, 2009

it was useful. thanks

By rojtv
on March 15, 2009

thank you

By elektronik
on March 15, 2009

thank you

By izolasyona
on March 15, 2009

thank you

By izolasyon
on March 15, 2009

thank you

By oyunoyna
on March 15, 2009

thank you

By karadeniz
on March 15, 2009

thank you

By çet
on March 15, 2009

thank you

By sohbetodaları
on March 15, 2009

thank you

By sohbet
on March 14, 2009

thank you

By Komik Videolar
on March 11, 2009

really good article

have a nice day

Chris

By haberler
on March 10, 2009

Thanks. Good news

By kredi
on March 10, 2009

thanks good article.

By izle
on March 9, 2009

thank u… ;)

By kalp
on March 9, 2009

i will test it..but still i dont understand … Thank you for sharing!!

perceft global hot=)

By mirc
on March 2, 2009

It’s very good article. Great site with very good look and perfect information
thankss

By msn
on March 2, 2009

nice tricks

By Crazy Driver
on February 25, 2009

it’s awesome! thanks for all

By Oyun
on February 25, 2009

thanks for you..!

By liseli serap
on February 25, 2009

thank you
good wp plugin

By KLM
on January 28, 2009

Great sharing thanks….

By Hero
on January 28, 2009

Thanks man

By chat
on January 19, 2009

Global chat sites

By Bedava Seyret
on January 18, 2009

i use somethings :)

By oyun indir
on January 18, 2009

ohhh! :D yes thanks admin for sharing

By Tr-bilgi
on January 18, 2009

Thanks

By cascas
on January 18, 2009

thanks….

By ganja
on January 18, 2009

nice site thank you admin.

By Any
on January 18, 2009

Thanks it is great !

By Dogu
on January 18, 2009

Thanks for all code

By idirfull
on January 18, 2009

thanks a lot

By download
on January 18, 2009

thanks man

By ibrahim
on January 17, 2009

thnk u mn ;)

By film izle
on December 22, 2008

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

By film indir
on December 16, 2008

thanks I search this code thnks

By Itzahk
on December 14, 2008

I was finding about css codes when I discovered this site. Congratulations for this website, It’s very useful and instructive. A lot of thanks!!

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