This post includes WordPress functions that I find myself using over and over again when creating themes for WordPress. These functions are for the most part basic, but they are very useful and every WordPress developer should know them.
1. wp_head()
Used in header.php, wp_head() triggers the wp_head hook that is pretty much required in order to run a smooth WordPress website. Plugins rely heavily on this function to make references to script files, jQuery libraries, stylesheets, etc. It must be place within the opening and closing head tags.
1 2 | <?php wp_head(); ?> </head> |
2. wp_footer()
Similar to wp_head(), wp_footer() is also used to call files required by many plugins. It must be placed right before the closing body tag.
1 2 3 | <?php wp_footer(); ?> </body> </html> |
3. bloginfo()
Used to display information about your site, such as site name, description, url, stylesheet url, template url, and more. Example:
1 2 3 4 | <?php bloginfo('name'); ?> <?php bloginfo('description'); ?> <?php bloginfo('stylesheet_url'); ?> <?php bloginfo('template_directory'); ?> |
4. get_header(), get_footer(), get_sidebar()
Functions used to include the template files header.php, footer.php, and sidebar.php respectively. They are the base of a WordPress template.
1 2 3 4 5 | <?php get_header(); ?> <?php get_sidebar(); ?> <?php get_footer(); ?> |
Note: You can also use name parameters to these functions in order to call for different template files. This is very handy when the layout of a website changes on different pages. For Example:
1 2 3 | <?php get_header('contact'); ?> <?php get_footer('contact'); ?> |
The example above would include template files named header-contact.php and footer-contact.php, allowing you to create custom headers and footers without altering your main header.php and footer.php files. It also works with get_sidebar().
5. is_front_page()
This is one of my favorites WordPress functions. It’s used to display something on the home page of a static front page setup under Appearance > Reading. Example:
1 2 3 | <?php if(is_front_page()) { your code goes here... } ?> |
6. is_home()
Very similar to is_front_page(), is_home is used to display stuff on the home page of a website with “latest posts” front page setup under Appearance > Reading. Example:
1 2 3 | <?php if(is_home()) { your code goes here... } ?> |
7. is_page()
Checks if a specific page or set of pages are being displayed. You can use page id, name, or slugs as parameters, like so:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | // Page Name <?php if(is_page('About Us')) { your code goes here... } ?> // Page ID <?php if(is_page(45)) { your code goes here... } ?> // Page Slug <?php if(is_page('about-us')) { your code goes here... } ?> // Pages with ID's 45, 60, 72 <?php if(array(is_page(45, 60, 72))) { your code goes here... } ?> // Pages About Us, Contact Us and Testimonials <?php if(array(is_page('About us', 'Contact Us', 'Testimonials'))) { your code goes here... } ?> |
8. is_single()
Targets the single post view. The page after you click to read a post. It also accepts post id, post title, and slug parameters.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?php if(is_single()) { your code goes here... } ?> <?php if(is_single('Why WordPress Rocks')) { your code goes here... } ?> <?php if(is_single('why-wordpress-rocks')) { your code goes here... } ?> <?php if(is_single(230)) { your code goes here... } ?> |
9. is_category()
Checks if a category archive page is being displayed. You can also target specific category pages by using parameters in the function. Those can be Category ID, title, slug or an array of ID’s, names and slugs. For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?php is_category(); // When any Category archive page is being displayed. is_category('15'); // When the archive page for Category ID 15 is being displayed. is_category('SEO'); // When the archive page for the Category with Name "SEO" is being displayed. is_category('web-design'); // When the archive page for the Category with Category Slug "web-design" is being displayed. is_category( array(15, 'web-design', 'SEO')); // Returns true when the category of posts being displayed is either term_ID 15, or slug "web-design", or name "SEO". ?> |
10. is_archive()
Checks if any type of archive page is being displayed
1 | <?php is_category(); ?> |
11. wp_list_pages()
Used to display pages as links on your site. You can arguments to display all pages, child pages of a specific page, order the pages, and more. Example:
1 | <?php wp_list_pages('&child_of=23&title_li='); ?> |
For a full list of arguments, visit the WordPress codex.
12. wp_list_categories()
Used to display a list of categories as links on your site. Example:
1 | <?php wp_list_categories('orderby=name&show_count=1&exclude=10'); ?> |
For a full list of arguments, visit the WordPress codex.