The Definitive WordPress Cheat Sheet
WordPress is arguably the most popular blogging platform on the planet. As pervasive as it is, however, it also comes with a level of complexity that can stymie all but the most savvy blog technicians among us.
But have no fear! Help is here for the rest of us mere mortals who can’t tell the difference between an index file and an index finger. Just point the latter to the definitive WordPress cheat sheet below and you’ll be coding and blogging like a WordPress Pro in no time!
WordPress Theme Anatomy
Before digging into the more technical stuff, it would be wise to familiarize yourself with the basic anatomy of a WordPress theme. For many bloggers, editing theme files sounds intimidating. But if you spend a little time familiarizing yourself with theme anatomy, you’ll have no problems knowing which file needs editing.
Theme files are located in Appearance > Editor. The amount of files will vary from theme to theme but the most basic theme will have the following files in place:
• style.css: this file contains all the styles associated with your theme. Use this file if you want to change the colors, fonts, margins, paddings, replace a background image, or change any other visual aspect of your theme.
• index.php: the index.php file controls what the homepage of your website looks like. By default it displays your blog posts in reverse chronological order. But if you set a static page to display as your homepage, then you need to create a separate page for your posts and in that case, index.php will generate the contents of your posts page.
• single.php: this file generates the contents of individual blog posts. It contains a loop that queries just one post and it allows you to specify which sidebars you want that post to use. Or you can remove sidebars from it altogether.
• archive.php, category.php, tag.php: these templates control the content displayed on your archive, category, and tag pages. If there is no archive.php file, the template will display the contents of index.php. The category.php will override archive.php for categories. In a similar fashion, tag.php will override archive.php for tags. You can use Category and Tag files to add text that shows a category or tag description, show excerpts instead of full posts, display images that link to your post, and more.
• searchform.php: this file controls the search form of your theme that is usually included in the sidebar of many themes and generates the search box form.
• search.php: displays the results when someone uses the search form on your site.
• 404.php: this file will display a 404 page if someone clicks on a post or page that is no longer available. You can add specific sidebars to display on your 404 page or add customized text to make better use of the page.
• comments.php: use the comments.php template if you want to modify how the comments are displayed on your site. Some comment plugins like Disqus will override the comments.php file completely.
• footer.php: the footer template controls the look of the footer area across the entire site and contains closing HTML tags.
• header.php: this file contains the opening HTML tags, the name and tagline for your blog and controls the overall appearance of header area. It also contains the primary navigation menu.
• sidebar.php: this file is responsible for displaying and controlling all the available widgets on your blog. You can add additional sidebars using the functions.php file and add content to them under Appearance > Widgets.
• page.php: the single page file generates the look of the individual pages. You can choose whether you want to have sidebars or remove them from the pages completely. You can create specific page templates using page.php by copying its contents into a new file, naming the new file whatever you want and adding the following at the top:
• functions.php: this file allows you to add your own custom PHP code if you want to modify core elements of your theme. As mentioned above, you can add new sidebars, change the number of characters in the excerpt, add a custom copyright notice, or add new sections to the admin panel.
Header Functions
As mentioned earlier, the header file controls how the area that holds your logo, blog name, blog description, and the primary navigation menu will appear. It also gives instructions to your theme where to find the stylesheet and other theme files. The code that generates the header is found in the header.php file and is called by the other templates with the following function:
This function also takes one parameter called name which is useful if you want to create different headers for different pages. The entire function then looks like this:
The header.php file accepts the following functions:
• bloginfo(‘name’): outputs the title of your blog set in Settings > General.
• wp_title(): outputs the title of a specific post or page.
• get_stylesheet_directory_uri(): returns the location of the stylesheet.
• bloginfo(‘pingback_url’): displays the Pingback XML-RPC file URL. The Pingback XML-RPC allows you to get notified when another blog has linked to one of your posts or pages. You can then choose to display this on your site.
• get_template_directory_uri(): this will return the location for the site’s theme files.
• bloginfo(‘version’): returns the WordPress version for the site and can be removed if you don’t want to display that information.
• bloginfo(‘atom_url’): returns the Atom for the site.
• bloginfo(‘rss2_url’): returns the RSS2 URL for the site. Both Atom and RSS2 display the feed of your latest blog posts in various feed readers.
• get_site_url(): returns the exact URL of your blog.
• bloginfo(‘description’): returns the description of your blog set in Settings > General.
• bloginfo(‘html_type’): this function will return the Content-Type of WordPress HTML pages which by default is “text/html.”
• bloginfo(‘charset’): displays the character encoding for pages and feeds set in Settings > Reading.
Navigation Menu
WordPress 3.0 introduced Custom Menus which allows you to add pages, posts, categories, and custom links to your menus. Using the functions below, you can add extra menus to your theme and display them where you want.
• register_nav_menus(): place this in your functions.php file to register a new menu.
• wp_nav_menu( array( ‘theme_location’ => ‘header-menu’ ) ): this will tell your theme where the new navigation menu should display.
Adding an example menu looks like this:
function register_new_menus() {
register_nav_menus(
array(
‘footer-menu’ => __( ‘Footer Menu’ ),
‘extra-menu’ => __( ‘Extra Menu’ )
)
);
}
add_action( ‘init’, ‘register_new_menus’ );
The wp_nav_menu supports additional parameters which can be added to the function so you can style the menu further. Here are some of the most useful parameters you can include in the wp_nav_menu function.
• menu_class: a CSS class to use for the ul element that forms the menu.
• menu_id: the ID that is applied to the ul element that forms the menu.
• container: this will add an extra wrapper around the ul that holds the menu. By default it is set to “div.”
• container_class: use this to specify a class that is applied to the container.
• container_id: if you want to apply an ID to your menu, use this parameter.
• before: this will add custom text before the link markup.
• after: this will display custom text after the link markup.
Template Functions
Template functions are used within different template files to retrieve and display different data. They’re called template tags and you can add them to any of the theme’s template files or custom templates that you create. Below is a list of the most useful template tags:
• the_content(): will retrieve and display the content of the posts.
• get_header(): this will retrieve the header.php file.
• get_sidebar(): this will retrieve the contents of the sidebar.php.
• get_footer(): returns the contents of the footer.php.
• the_time(‘m-d-y’): returns the date in month-day-year format.
• comments_popup_link(): retrieves link for the comments on the post.
• the_title(): returns the title of a specific post or page.
• the_permalink(): returns the URL of a specific post or page.
• has_post_thumbnail(): checks if the post has an image attached.
• the_post_thumbnail(): displays the post thumbnail.
• the_category(): displays the category of a specific post or page.
• the_author(): displays the name of the author of a specific post or page.
• the_author_link(): displays either author’s link or author’s name.
• the_author_meta(): displays the information entered in User Profile.
• the_excerpt(): returns the excerpt for a post.
• the_ID(): returns the ID of a specific post or page.
• edit_post_link(): displays the link to edit a specific post or page.
• comments_template(): displays the comment.php file’s content.
• wp_list_pages(): lists the pages of the site.
• wp_list_cats(): lists the categories for the site.
• next_post_link(‘%link’): returns the URL to the next post.
• previoust_post_link(‘%link’): returns URL to the previous post.
• posts_nav_link(): returns next and previous post links.
The Loop
The loop is the most powerful part of any WordPress theme. First, it queries the database to determine if there are posts or pages to show. Then it formats the post or a page according to the theme’s instructions or ones you set. The loop ends with an “endwhile” statement. Everything in between the initial query and the final “endwhile” statement is up to you.
You can add code that determines the output of titles, whether the post displays at full length or as an excerpt, how metadata should be displayed, and anything else you can think of. Each page can have multiple loops and queries.
The entire loop looks like this:
Here are the functions used to start and end the loop as well as the most popular template tags you can use in it:
• if(have_posts()): checks if there are posts.
• while(have_posts()):the_post(): shows posts as long as posts are available.
• endwhile: closes the “while” PHP function.
• endif: closes the “if” PHP function.
• the_title(): outputs the title of the post.
• the_excerpt(): outputs the post excerpt.
• the_content (): outputs the full post content.
• the_excerpt(): outputs the first 55 words of a post’s main content followed by an ellipsis (…).
• the_category(): outputs the post categories.
• the_tags(): outputs the tags associated with the post.
• the_author(): outputs the post author.
• the_date(): outputs the post date.
Miscellaneous Code Snippets
Aside from the most important functions mentioned above, WordPress has plenty of useful functions and code snippets that will allow you to go beyond the basics and fine-tune your blog.
• /%postname%/: used to display a custom permalink instead of “ugly URLs” that WordPress generates by default.
• include(TEMPLATEPATH . ‘/template-name.php’): include any file in your theme.
• _e(‘Message’): prints out a custom message.
• wp_register(): displays the register link.
• wp_loginout(): displays the login/logout link.
• wp_meta(): displays meta for administrators.
• timer_stop(1): displays time to load the page.
• echo get_num_queries(): displays queries to load the page.
• remove_comment_fields(): removes a specified field from the comment form.
• wp_enqueue_script(): use this to enqueue stylesheets or JavaScript files to use them with your theme.
This article originally appeared on The Art of Blog. Republished with permission.1>