Skip to content

Instantly share code, notes, and snippets.

@loorlab
Created October 20, 2015 07:28
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save loorlab/3278c30b964567bc82d0 to your computer and use it in GitHub Desktop.
Save loorlab/3278c30b964567bc82d0 to your computer and use it in GitHub Desktop.
Wordpress cheat sheets
1. Theme Structure
- style file
style.css
- header section
header.php
- main section
index.php
- sidebar section
sidebar.php
- footer section
footer.php
- static front page
front-page.php
- display tags
tag.php
- display category page
category.php
- post template
single.php
- page template
page.php
- comments template
comments.php
- search content
search.php
- search form
searchform.php
- archive
archive.php
- special functions
functions.php
- error page
404.php
Note: 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.
2. Functions
- root url for website
<?php site_url(); ?>
- title of specific post/page
<?php wp_title(); ?>
- title of site
<?php bloginfo('name'); ?>
- site description
<?php bloginfo('description'); ?>
- stylesheet folder location
<?php get_stylesheet_directory(); ?>
- style.css file location
<?php bloginfo('stylesheet_url'); ?>
- pingback url
<?php bloginfo('pingback_url'); ?>
- template folder path
<?php bloginfo('template_url'); ?>
- wordpress blog version
<?php bloginfo('version'); ?>
- atom url
<?php bloginfo('atom_url'); ?>
- rss2 url
<?php bloginfo('rss2_url'); ?>
- root url for website
<?php bloginfo('url'); ?>
- html version
<?php bloginfo('html_type'); ?>
- charset parameter
<?php bloginfo('charset'); ?>
3. Template Functions
- content of posts/pages
<?php the_content(); ?>
- check if there are posts
<?php if(have_posts()): ?>
- shows posts
<?php while(have_posts()): the_post(); ?>
- closes loop
<?php endwhile; ?>
- closes if
<?php endif; ?>
- header.php file contents
<?php get_header(); ?>
- sidebar.php file contents
<?php get_sidebar(); ?>
- footer.php file contents
<?php get_footer(); ?>
- the date is '08-18-07'
<?php the_time('m-d-y'); ?>
- link to comments of post
<?php comments_popup_link(); ?>
- title of post/page
<?php the_title(); ?>
- url of post/page
<?php the_permalink(); ?>
- category of post/page
<?php the_category(); ?>
- author of post/page
<?php the_author(); ?>
- id of post/page
<?php the_ID(); ?>
- edit link of post/page
<?php edit_post_link(); ?>
- links from blogroll
<?php wp_list_bookmarks(); ?>
- comment.php file contents
<?php comments_template(); ?>
- list all pages
<?php wp_list_pages(); ?>
- list all categories
<?php wp_list_categories(); ?>
- url to next post
<?php next_post_link('%link'); ?>
- url to previous post
<?php previous_post_list('%link'); ?>
- show post calendar
<?php get_calendar(); ?>
- list of archive urls
<?php wp_get_archives(); ?>
- next and previous post link
<?php posts_nav_link(); ?>
- rewinds post for a second loop
<?php rewind_posts(); ?>
4. Extra Functions
- custom permalinks
/%postname%/
- include file from template folder
<?php include(TEMPLATEPATH . '/x'); ?>
- value returned from search from
<?php the_search_query(); ?>
- return translated text from translate()
<?php _e('Message'); ?>
- register link
<?php wp_register(); ?>
- login/logout link
<?php wp_loginout(); ?>
- admin meta data
<?php wp_meta(); ?>
- start page timer (header.php)
<?php timer_start(); ?>
- time to load the page (footer.php)
<?php timer_stop(1); ?>
- show queries executed to generate page
<?php echo get_num_queries(); ?>
5. The Loop
<?php if(have_posts()){ ?>
<?php while(have_posts()){ ?>
<?php the_post(); ?>
<?php //custom post content code for title, excerpt and featured image ?>
<?php } //end while ?>
<?php } //end if ?>
Note: 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.
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.
<?php if(have_posts()) : ?>
<?php while(have_posts()) : the_post(); ?>
// Custom HTML & PHP code
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>
6. Template Include Tags
- Include header.php file
<?php get_header(); ?>
- Include sidebar.php file
<?php get_sidebar(); ?>
- Include footer.php file
<?php get_footer(); ?>
- Include comments.php file
<?php comments_template(); ?>
Note: 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.
7. Template Bloginfo Tags
- Title of the blog
<?php bloginfo('name') ; ?>
- Displays the character set
<?php bloginfo('charset'); ?>
- Displays the description of the blog
<?php bloginfo('description'); ?>
- Displays the address of the blog
<?php bloginfo('url'); ?>
- Displays the RSS URL
<?php bloginfo('rss2_url'); ?>
- Displays the URL of the template
<?php bloginfo('template_url'); ?>
- Displays the pingback URL
<?php bloginfo('pingback_url'); ?>
- Displays the URL for the template's CSS file
<?php bloginfo('stylesheet_url'); ?>
- Displays URL for WordPress installation
<?php bloginfo('wpurl'); ?>
- Displays the "Site Title" set in Settings > General
<?php bloginfo('name'); ?>
Note: These tags are used to display information regarding your blog,
information that can be customized inside the Wordpress Administration panel. http://codex.wordpress.org/Function_Reference/bloginfo
8. Wordpress Conditional Tags
- when the user is on the home page(blog)
is_home()
- when the user is on the home page (blog or page)
is_front_page()
- when a single post is displayed
is_single
- check if a post is sticky
is_sticky()
- when a page is displayed
is_page()
- when a categoryis displayed
is_category()
Note: 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.
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
9. Common Wordpress Tags
Displays the time of the current post
<?php the_time() ?>
Displays the date of a post or set of posts
<?php the_date() ?>
Displays or returns the title of the current post
<?php the_title(); ?>
Displays the URL for the permalink
<?php the_permalink()?>
Displays the category of a post
<?php the_category() ?>
Displays the author of the post
<?php the_author(); ?>
Displays the numeric ID of the current post
<?php the_ID(); ?>
Displays all the pages
<?php wp_list_pages(); ?>
Displays a tag cloud
<?php wp_tag_cloud(); ?>
Displays the categories
<?php wp_list_cats(); ?>
Displays the calendar
<?php get_calendar(); ?>
Displays a date-based archives list
<?php wp_get_archives() ?>
Displays Previous page and Next Page links
<?php posts_nav_link(); ?>
Displays Newer Posts link
<?php next_post_link() ?>
Displays previous link
<?php previous_post_link() ?>
Note: 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.
10. Wordpress Navigation Menu
<?php wp_nav_menu(); ?>
Specific navigation menu
<?php wp_nav_menu( array('menu' => 'Project Nav' )); ?>
Note: 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
<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
<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>
Note: 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.
11. Display X posts from a category
<?php query_posts('category_name=Name Here&showposts=10'); ?>
Note: 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.
The name should be exactly the same as the one typed in the Administration panel under categories section.
12. Custom Template File
<?php include (TEMPLATEPATH . '/searchform.php'); ?>
Note: 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.
/* WP CHEAT SHEET ADVANCED */
1. Content Only for The Home Page
<?php
if(is_home())
{
include ('example.php');
}
?>
Note: This snippet will include the file specified, only if the user is on the home page of the site.
Place this code in the index.php file.
2. Styiling Different Categories
<?php
if (is_category('15')
{
<link rel="stylesheet" href="<?php bloginfo('template_url*); ?>/cat-15.css" media="screen">
<?php } else { ?>
<link rel="stylesheet" href="<?php bloginfo(stylesheet_url*); ?>" media="screen">
<?php } ?>
Note: This snippet assigns a specific stylesheet (category-15.css) to category 15 and will assign the rest of the site the default stylesheet (style.css).
Place this code in the <head> area.
3. Previous & Next Posts Links
<?php next_posts_link('Next Entries &raquo;') ?> <?php previous_posts_link('&raquo; Older Entries'); ?>
Note: This snippet will echo "Next Entries >>" with a link to the next set of posts.
The second snippet will echo "<< Previous Entries" with a link to the previous set of posts.
Place this code outside the loop.
4. Dyniamic Page Titles
<?php
if (is_home()){
echo bloginfo('name');
}
elseif (is_404())
{
echo 'WPCandy » 404';
}
elseif (is_search())
{
echo 'WPCandy » Search Results';
}
else
{
echo 'WPCandy » ';wp_title('');
}
?>
Note: If the home page is active, the title will display the name of the site. If the 404 page is active, the title will echo 'WPCandy » 404'.
If the Search Results page is active, the title will echo ‘WPCandy » Search Results’.
If any other page on the site is active, the title will display
5. CSS Theme Details
/*
Theme Name: Your theme name goes here
Description: Your theme description goes here
Theme URL: Your URL website
Version: 1.0
Author: Your name
Author URL: Your URL blog
Template: Degine a parent template (optional)
Tag: Your tag website or theme
*/
Note: This snippet defines a theme.
WordPress will read this and assign it to the theme.
Use the 'Template:' to define a parent template for the theme.
Place this code at the top of the syle.css file.
6. The Loop
<?php if (have_posts()) : ?>
<?php while(have_posts()) : the_post();?>
// Post content here (Custom HTML & PHP code)
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>
Note: This snippet is the basic form of the loop.
WordPress will read the code between the beginning of the loop and the end of the loop, and display it accordingly on each post or page.
Any HTML or PHP placed inside the loop will be used for each post.
Place this code in any
7. Unique Image for Different Categories <?php if (is_category('7') ):
<img src="<?php bloginfo('template_url');?>/images/cat7.jpg' alt=" /> <?php elseif (is_category('8') ):
<img src="<?php bloginfo('template_url');?>/images/cat8.jpg' alt=" /> <?php endif; ?>
Note: This snippet assings an image (cat 7.jpg) next to each post title in the category 7 and an image (cat8.jpg) next to each post title in category 8.
Place this code in the category.php
8. Styling Individual Posts
<div id="post-<?php the_ID();?>">
Note: This snippet will assign the post ID to the DIV.
For example, if the ID for the post is 8, that line will echo as <div id="post-8"></div>.
Now you can style that individual post in the CSS as #post-8.
Place this code within the loop.
9. Site Page Links
<ul>
<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>
Note: This snippet will first echo the text "home" with a link to the home page.
Next, it will echo the WordPress pages links in a list, in order defined by your settings, excluding the child pages, and excluding a title header for the list.
If one of the pages in the list is active, the link for that page will be assigned the class"corrent_page_item", which can now be styled
10. Query Posts
<?php query_posts('cat=2&showposts=5');?>
Note: This snippet will display the 5 latest posts from the only category 2.
Place this code right before the loop.
11. Page Template Details
<?php
/* Template Name: Gallery */
?>
Note: This snippet defines a page template.
You will also need to have a corresponding file to use as the new page template.
For example, in this case, you would create a gallery.php file.
12. Unique Template for Categories
<?php
$post = $wp_query- >post;
if(in_category('3'))
{
include(TEMPLATEPATH . '/cat3.php’);
}
elseif(in_category('4'))
{
include(TEMPLATEPATH . '/cat4.php');
}
else
{
include(TEMPLATEPATH . '/cat.php');
}
?>
Note: This snippet will assign a uniqe template to certain categories.
In this case, 'cat3.php' will be assigned to 'Category3', 'cat4.php' will be assigned to Category 4', and the rest of the categories will be assigned 'cat.php'.
Place this code at the top of category.php.
13. Tags Cloud
<?php wp_tag_cloud('smallest=1&largest=9&'); ?>
Note: This snippet will create a tag cloud, in alphabetical order, with the smallest text at 1 pt and the largest text at 9 pts.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment