Themes Files – WordPress Themes Development Part 1

 Themes Files – WordPress Themes Development Part 1


Themes files are used in all WordPress themes, but first let’s get to know the terminology.


Theme file terminology The term “theme” is used in different ways when working with WordPress themes:

Template files are located inside the themes and express how your site will be displayed.

  • Page Templates (Page Templates) are those that apply only to the pages to change their shape and style. A page template can be applied to a single page, a page section, or a category of pages.
  • Template tags are built-in WordPress functions that you can use inside a template file to retrieve and display data (such as the_title() and the_content()).
  • Template Hierarchy is the logic that WordPress uses to select the template template file(s) to use, depending on the desired content.

Common WordPress template files #Common WordPress template files

Below is a list of some basic theme templates and files recognized by WordPress.

index.php

The main template file. It is required in all themes.

style.css

The main stylesheet. It is required in all themes and contains the information header for your theme.

rtl.css

The right-to-left stylesheet is included automatically if the website language’s text direction is right-to-left.

comments.php

The comments template.

front-page.php

The front page template is always used as the site front page if it exists, regardless of what settings on Admin > Settings > Reading.

home.php

The home page template is the front page by default. If you do not set WordPress to use a static front page, this template is used to show latest posts.

header.php

The header template file usually contains your site’s document type, meta information, links to stylesheets and scripts, and other data.

singular.php

The singular template is used for posts when single.php is not found, or for pages when page.php are not found. If singular.php is not found, index.php is

single.php

The single post template is used when a visitor requests a single post.

single-{post-type}.php

The single post template used when a visitor requests a single post from a custom post type. For example, single-book.php would be used for displaying single posts from a custom post type named book. The index.php

used if a specific query template for the custom post type is not present.

archive-{post-type}.php

The archive post type template is used when visitors request a custom post type archive. For example, archive-books.php would be used for displaying an archive of posts from the custom post type named books. The <

archive.php template file is used if the archive-{post-type}.php is not present.

page.php

The page template is used when visitors request individual pages, which are a built-in template.

page-{slug}.php

The page slug template is used when visitors request a specific page, for example one with the “about” slug (page-about.php).

category.php

The category template is used when visitors request posts by category.

tag.php

The tag template is used when visitors request posts by tag.

taxonomy.php

The taxonomy term template is used when a visitor requests a term in a custom taxonomy.

author.php

The author page template is used whenever a visitor loads an author page.

date.php

The date/time template is used when posts are requested by date or time. For example, the pages generated with these slugs:
http://example.com/blog/2014/
http://example.com/blog/2014/05/
http://example.com/blog/2014/05/26/

archive.php

The archive template is used when visitors request posts by category, author, or date. Note: this template will be overridden if more specific templates are present like category.phpauthor.php, and date.php.

search.php

The search results template is used to display a visitor’s search results.

attachment.php

The attachment template is used when viewing a single attachment like an image, pdf, or other media file.

image.php

The image attachment template is a more specific version of attachment.php and is used when viewing a single image attachment. If not present, WordPress will use attachment.php instead.

404.php

The 404 template is used when WordPress cannot find a post, page, or other content that matches the visitor’s request.

Template files #

WordPress templates consist of multiple files. These are PHP files that contain a mixture of HTML, template tags , and PHP code.

When you build your theme, you will use template files to influence the layout and design of different parts of your website. For example, you can use the header.php template to create your header or the comments.php template to include comments.

When someone visits a page on your website, WordPress uploads a template upon request. The type of content displayed in the theme file is determined by the post type associated with the theme file. The template hierarchy describes the template file that will be loaded by WordPress based on the type of request and whether the template is present in the theme. The server then parses the PHP in the template and returns the HTML to the visitor.

The most important template file is index.php , which is a global template if a more specific template cannot be found in the theme hierarchy. Although the theme only needs an index.php template , a theme usually includes several templates for displaying different content types and contexts.

#Using Templates


Within WordPress themes, you can use theme tags to display information dynamically, include other template files, or customize your site in any other way.

For example, in your index.php file, you can include other files in the final page created:

To include the header , use get_header()
to include the sidebar , use get_sidebar()
to include the footer , use get_footer()
to include the search form , use get_search_form()
to include custom theme files , use get_template_part()

Here is an example of WordPress template tags to include specific templates on your page:

<?php get_sidebar(); ?>
<?php get_template_part( 'featured-content' ); ?>
<?php get_footer(); ?>