Day 41: Working with the themes

WordPress template hierarchy (visual overview):

Home Page Display:

By default, WordPress sets your site’s home page to display your latest blog posts. This page is called the blog posts index. You can also set your blog posts to display on a separate static page.

  • home.php: Used to render the blog posts index, whether it is being used as the front page or on a separate static page. If home.php does not exist, WordPress will use index.php.
  • index.php: Default fallback for rendering the blog posts index.
  • front-page.php: Overrides home.php if exists.

Front Page Display:

The front-page.php template file is used to render your site’s front page, whether it displays the blog posts index or a static page.

  • front-page.php: Renders the front page, taking precedence over the blog posts index template.
  • home.php: Used if front-page.php does not exist and the front page displays the blog posts index.
  • page.php: Used when the front page is set as a static page.

Privacy Policy Page Display:

The privacy-policy.php template file is used to render your site’s Privacy Policy page.

  • privacy-policy.php: Renders the Privacy Policy page, taking precedence over other static page templates.
  • custom template file: Page template assigned to the Privacy Policy page.

Single Post Display:

The single post template file is used to render a single post.

  • single-{post-type}-{slug}.php
  • single-{post-type}.php
  • single.php
  • singular.php
  • index.php

Single Page Display:

The template file used to render a static page (page post-type).

  • custom template file
  • page-{slug}.php
  • page-{id}.php
  • page.php
  • singular.php
  • index.php

Category Display:

Rendering category archive index pages.

  • category-{slug}.php
  • category-{id}.php
  • category.php
  • archive.php
  • index.php

Tag Display:

To display a tag archive index page.

  • tag-{slug}.php
  • tag-{id}.php
  • tag.php
  • archive.php
  • index.php

Custom Taxonomies Display:

Custom taxonomies use a slightly different template file path.

  • taxonomy-{taxonomy}-{term}.php
  • taxonomy-{taxonomy}.php
  • taxonomy.php
  • archive.php
  • index.php

Custom Post Types Display:

Custom Post Types use the following path to render the appropriate archive index page.

  • archive-{post_type}.php
  • archive.php
  • index.php

Author Display:

Rendering author archive index pages.

  • author-{nicename}.php
  • author-{id}.php
  • author.php
  • archive.php
  • index.php

Date Display:

Date-based archive index pages.

  • date.php
  • archive.php
  • index.php

Search Result Display:

Search results follow the same pattern as other template types.

  • search.php
  • index.php

404 (Not Found) Display:

404 template files are called in this order.

  • 404.php
  • index.php

Attachment Display:

Rendering an attachment page.

  • {MIME-type}.php
  • single-attachment-{slug}.php
  • single-attachment.php
  • single.php
  • singular.php
  • index.php

Embeds Display:

The embed template file is used to render a post which is being embedded.

  • embed-{post-type}-{post_format}.php
  • embed-{post-type}.php
  • embed.php
  • wp-includes/theme-compat/embed.php

Non-ASCII Character Handling:

Since WordPress 4.7, any dynamic part of a template name which includes non-ASCII characters in its name supports both the un-encoded and the encoded form.

Filter Hierarchy:

The WordPress template system lets you filter the hierarchy.

Common Theme Functions

  1. wp_head(): This function is used to output crucial information for the head section of your theme, such as stylesheets, scripts, and meta tags. It’s typically called within the <head> section of your theme’s header.php file.
   <?php wp_head(); ?>
  1. wp_footer(): Similar to wp_head(), this function outputs essential scripts and markup for the footer section of your theme. It’s usually called just before the closing </body> tag in your theme’s footer.php file.
   <?php wp_footer(); ?>
  1. get_header(): This function fetches the header.php template file of your theme. It’s commonly used to include the header section in other template files, like index.php, single.php, etc.
   <?php get_header(); ?>
  1. get_footer(): Similar to get_header(), this function includes the footer.php template file of your theme into other template files.
   <?php get_footer(); ?>
  1. get_sidebar(): This function fetches the sidebar.php template file of your theme. It’s used to include a sidebar section in other template files.
   <?php get_sidebar(); ?>
  1. body_class(): This function adds classes to the <body> tag based on the current page, post, or category being viewed. It’s typically used in the body tag of your theme’s header.php file.
   <body <?php body_class(); ?>>
  1. post_class(): Similar to body_class(), this function adds classes to the post div based on the current post being displayed. It’s usually used within the loop in template files like single.php and index.php.
   <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
  1. the_post_thumbnail(): This function displays the featured image (post thumbnail) of the current post within the loop. It’s commonly used in template files like single.php and archive.php.
   <?php the_post_thumbnail(); ?>
  1. the_content(): This function outputs the content of the current post within the loop. It’s used to display the main content of a post or page.
   <?php the_content(); ?>
  1. the_title(): Similar to the_content(), this function outputs the title of the current post or page within the loop. <?php the_title(); ?>

Leave a Reply

Your email address will not be published. Required fields are marked *