My WordPress Cheat Sheet

wordpressI know that there are many resources regarding this topic but there are never enough. This post is dedicated to small snippets from WordPress that will make your life easier. Or maybe my life easier and in this case I want to have them in one single post. In a way I’m trying to help you and in another I’m trying to organize this stuff for myself.

Theme Structure

If you want to create a WordPress theme, these following files must be included in order to be a standard theme. You can create a theme using fewer files but this is the way to do it.

1
2
3
4
5
6
7
8
9
10
11
12
header.php - header section
index.php - main section
sidebar.php - sidebar section
footer.php - footer section
single.php - post template
page.php - page template
comments.php - comments template
search.php - search content
searchform.php - search form
archive.php - archive
functions.php - special functions
404.php - error page

The Loop

You will often see “the loop” as reference in many tutorials or samples. This piece of code helps you display your posts on a blog. By entering custom HTML or PHP code inside the loop, you will make every post to benefit from that custom code. You can use the loop mainly in your index.php file but also in other files when you want to display multiple posts.

1
2
3
4
5
6
<?php if(have_posts()) : ?>
   <?php while(have_posts()) : the_post(); ?>
// Custom HTML & PHP code
   <?php endwhile; ?>
<?php else : ?>
<?php endif; ?>

Note: the space in front of ?php on the line 1,2,4,5 and 6 should be removed. So instead of < ?php we will have <?php.

Template Include Tags

These tags are usually used in a single PHP file to include other files from the theme. For example you can use the get_header tag in index.php in order to include the head in the theme.

1
2
3
4
< ?php get_header(); ?>
< ?php get_sidebar(); ?>
< ?php get_footer(); ?>
< ?php comments_template(); ?>

Template Bloginfo Tags

These tags are used to display information regarding your blog, information that can be customized inside the WordPress Administration panel.

1
2
3
4
5
6
7
8
9
10
< ?php bloginfo('name'); ?> - Title of the blog
< ?php bloginfo('charset'); ?> - Displays the character set
< ?php bloginfo('description'); ?> - Displays the description of the blog
< ?php bloginfo('url'); ?> - Displays the address of the blog
< ?php bloginfo('rss2_url'); ?> - Displays the RSS URL
< ?php bloginfo('template_url'); ?> - Displays the URL of the template
< ?php bloginfo('pingback_url'); ?> - Displays the pingback URL
< ?php bloginfo('stylesheet_url'); ?> - Displays the URL for the template's CSS file
< ?php bloginfo('wpurl'); ?> - Displays URL for WordPress installation
< ?php bloginfo('name'); ?>

WordPress Conditional Tags

Conditional tags are simple but helpful tags that can be used to customize how your blog will work. For example if the page is the home page, we will type a class called “current-cat”. < ?php if(is_home()) { ?> class=”current-cat”< ?php } ?>. This is a part of the code which I will present you a little bit later in this article.

1
2
3
4
5
6
is_home() - when the user is on the home page(blog)
is_front_page() - when the user is on the home page (blog or page)
is_single - when a single post is displayed
is_sticky() - check if a post is sticky
is_page() - when a page is displayed
is_category() - when a category is displayed

These are the most common conditional tags inside WordPress. For more information and additional tags you can check the next address dedicated to conditional tags. http://codex.wordpress.org/Conditional_Tags

Common WordPress Tags

As you know WordPress has a lot of code that can be embedded in themes in order to make them complex and powerful. Here are some of the common snippets that are used in most of the templates.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
< ?php the_time() ?> - Displays the time of the current post
< ?php the_date() ?> - Displays the date of a post or set of posts
< ?php the_title(); ?> - Displays or returns the title of the current post
< ?php the_permalink() ?> - Displays the URL for the permalink
< ?php the_category() ?> - Displays the category of a post
< ?php the_author(); ?> - Displays the author of the post
< ?php the_ID(); ?> - Displays the numeric ID of the current post
< ?php wp_list_pages(); ?> - Displays all the pages
< ?php wp_tag_cloud(); ?> - Displays a tag cloud
< ?php wp_list_cats(); ?> - Displays the categories
< ?php get_calendar(); ?> - Displays the calendar
< ?php wp_get_archives() ?> - Displays a date-based archives list
< ?php posts_nav_link(); ?> - Displays Previous page and Next Page links
< ?php next_post_link() ?> - Displays Newer Posts link
< ?php previous_post_link() ?> - Displays previous link

WordPress Navigation Menu

This thing is different based on how you want your blog to work. You can have a menu based on pages, on categories or on both. In every way you will need a home page link. In this case here the the 2 approaches for the menu.

Categories based menu

1
2
3
4
5
<ul id="menu">
<li <?php if(is_home()) { ?> class="current-cat"< ?php } ?>>
<a href="<?php bloginfo('home'); ?>">Home</a></li>
< ?php wp_list_categories('title_li=&orderby=id'); ?>
</ul>

Pages based menu

1
2
3
4
5
<ul id="menu">
<li <?php if(is_home()) { ?> class="current_page_item"< ?php } ?>>
<a href="<?php bloginfo('home'); ?>">home</a></li>
< ?php wp_list_pages('sort_column=menu_order&depth=1&title_li='); ?>
</ul>

In both cases we add a class that is used by WordPress in styling the list items. So, in this case we will add the classes to a hardcoded home list item.

Display X posts from a category

On the first page we have in the sidebar 2 sections for latest tips and latest graphic ratings. Those sections were made with the help of the query_posts.

1
< ?php query_posts('category_name=Name Here&showposts=10'); ?>

The name should be exactly the same as the one typed in the Administration panel under categories section.

Custom Template File

In WordPress you can insert any additional template file that is none of the ones in the first section. In this way you can make your own template file and embed it in your theme.

1
< ?php include (TEMPLATEPATH . '/searchform.php'); ?>

Final words
This is my WordPress Cheat Sheet. For more information, you can search the WordPress Codex. You can find there all the information you need: tags, parameters, functions, hacks etc. Hopefully this page will help you and it will give you a little more time in designing instead of researching. Thanks WordPress and cheers!

47 ideas on “My WordPress Cheat Sheet

  1. szek

    great collection of WP tags & tips. thanks for your effort!

  2. Gian

    Nice cheat sheet. All useful code are in it. Great!!

  3. A.

    would be handy if you made into a pdf to download, let me know

  4. Chris Wallace

    Don’t forget about wp_list_comments() as well.

  5. Chris Wallace

    you also need to add get_search_form()

  6. David Bisset

    Great work, something I think should have been in the WordPress Codex for some time. Should also make it into some sort of Dreamweaver Shortcut file (I have made my own version).

  7. teddY

    Wow, very interesting cheat sheet! I like it that you’ve selectively shared the WP codes that are more common, and excluding uncommon ones, for example, is_feed() from the conditional tags section. You’ve definitely invested a lot of effort in filtering through your own WP knowledge and the codex :) kudos!

    Thank you so much for sharing!

  8. Timothy

    Nice. You should make this into a PDF. It’s good advertising for your site as well (since you would put your site’s name & url in it).

    • Andy Gongea

      In a few days these snippets will be available in a PDF file.

  9. BeyondRandom

    Very nice cheat sheet! Bookmarked and will use regularly. Thanks.

  10. Andy Gongea

    @Chris Those 2 templates tags are implemented in 2.7 and are not so common. The main purpose for this was displaying most common tags. Maybe in the future I will digg and I will make a reload version.

    @Teddy – Thanks. I put a little effort in this article, you are right. (I like ur blog)

    @David I’ll think about it. Thanks. Great resources you’ve got there on your blog! Cheers!

  11. alleks

    Wow, thank you for sharing this! Useful info!

  12. Skid Vis

    In the meanwhile.. here’s pdf version:
    http://skidvis.com/WordpressCheatSheet.pdf

  13. Sujhon

    Very useful.. and in details ..good one

  14. feir

    Very useful. Providing a PDF file will be much more nice :)

  15. Frank

    The Page Menu ist easy with version 2.7 of WordPress:

    <?php if ( function_exists('wp_page_menu') ) { // erst ab wp 2.7
    wp_page_menu('show_home=1&menu_class=page-navi&title_li=');
    } else {
    wp_list_pages('title_li=' . __('Seiten', 'basis') . '');
    } ?>

    • Andy Gongea

      Agree

  16. framejockey

    I have knocked up a pdf, download at http://www.framejockey.co.uk/acrobat/wordpressCS.pdf. Hope thats ok, let me know if not and I’ll take it down .

  17. Susan

    Wow, what an awesome list!

  18. Deauxmain

    Thanks so much for the info and the PDF cheatsheet. Good stuff!

    • Vijay

      Nice tips. It would be better if you distribute as pdf.

  19. Carla

    Great! This is exactly what I needed.

  20. framejockey

    ViJay……….

    ‘Nice tips. It would be better if you distribute as pdf’

    look up 2 posts……tada !

  21. Vijay

    WOW, pdf link is there in the comments.

    Thanks framejockey to point this out.

    Thanks to Skid Vis for the PDF version.

  22. Geoserv

    This is fantastic, thanks for sharing.

  23. Nihar

    Thanks for the wordpress cheat sheet. I will bookmark this page.

  24. John

    Great Cheat Sheet! I’ve bookmarked it, and I’m sure I’ll use this as a reference on all my future WordPress projects!

  25. Poker Blog

    Thanks for the cheat sheets, will come in handy on my next blog. Thumbs up on stumble from me.

  26. Rootsantic

    Nice little WordPress cheat sheet, http://tinyurl.com/7mhseg

    This comment was originally posted on Twitter

  27. Rob H

    Clear and simple ‘help sheet’ – really useful, thanks

  28. WonderWy

    @ThemeForest WP bookmarks http://bit.ly/cDlwQ http://bit.ly/lU526 http://bit.ly/1128OB http://bit.ly/hV2vJ

    This comment was originally posted on Twitter

  29. china

    really, really very nice,I am a novice, I came to learn. thanks for sharing information!!! ^0^

  30. andygongea

    My WordPress Cheat Sheet – http://is.gd/N9Rc – A great resource for WordPress web designers & developers

    This comment was originally posted on Twitter

  31. aliplanning

    Thank you to share

  32. resourcesmix

    great code Snippets, thanks for sharing!

  33. vivek bandebuche

    Really great job, very useful and easy to understand. It’s a must-read ;)

  34. chinaphone

    this is useful.thanks!

  35. shoaib

    this is awesome,will help me a lot as m new to wp.thnx for this

  36. Ivan

    Realy good job!!!

  37. Lalo

    good job, but you forget the category template page :) (category.php)

  38. airyland

    Thanks for your work!

  39. quicklycode
  40. permatec

    Thanks for sharing WP theme base article.I am not a coder but i think your article helps many developer…

  41. Houston Cosmetic dentist guy

    I am now trying to save some money and get my site build freely with the wordpress editing. Your tag will certainly help me.

  42. iVINI dESIGNER

    Molto buono, Grazie !

  43. Khyne

    Ah i love cheat sheets. Thanks for sharing