banner



My Theme Does Not Have An Index.php Template?

The Ultimate Guide to Theme Template Files in WordPress

If y'all've always delved under the hood of a WordPress theme, y'all'll know that it contains four kinds of files: the stylesheet, the functions file, a number of template parts, and one or (most probable) more than template files.

For a theme to work, it must take at least two files: the stylesheet and an index.php template file. Just most themes will have more than template files than that, each of which is designed to display different kinds of content.

In this post, I'll explicate which template files do what and how WordPress decides which template file to utilise when displaying a given folio on your site.

Continue reading, or leap ahead using these links:

  • The Kinds of Template File
  • The Template Hierarchy
  • Unmarried Template Files in Detail
  • Archive Template Files in Particular
  • The Home Page and the Main Weblog Page
  • Special Templates in Detail
  • Template Parts in Detail

The Kinds of Template File

There are six kinds of template files which you lot can use in your themes. These are:

  • Template files for displaying individual posts, pages or other post types.
  • Template files for displaying athenaeum, such as category archives, tag athenaeum, date archives, author archives
  • The template files for the home folio and /or the principal web log folio, front-page.php and home.php.
  • Template files for displaying special pages such as the 404 page.
  • The index.php file, which can be used to display single posts or archives in the absence of a more specific template file.
  • Template parts. These aren' t really template files but they are worth mentioning equally you will use them within your template files.

In a moment I'll work through each of these in turn merely first it helps to know how WordPress selects template files.

The Template Bureaucracy

Before looking at the unlike kinds of template files in detail, it's helpful to know exactly how WordPress decides which one to apply on any given page on your website.

WordPress uses the template bureaucracy, which is a listing of the different kinds of template files, in social club. WordPress will ever employ the most specific file for the content type being displayed, with index.php equally the final fallback. Which is why every theme needs an index.php file – in theory, you could just have that file and utilize information technology to display all of the content in your site. In practice, it's better to employ multiple template files.

The WordPress template hierarchy

Here'south an instance to illustrate how information technology works.

Let's say someone is viewing the 'WordPress' category archive on your site. WordPress volition look for the relevant template file in a detail order, and use the starting time 1 it finds to display the content:

  1. A category template file for that specific category, which is named using the category slug. In this case, it would be category-wordpress.php.
  2. A general category template file: category.php.
  3. An archive template file: annal.php.
  4. The index file: index.php.

So if your theme has an archive.php file and a category.php file as well asindex.php, it will use category.php.

Now let's take a look at your contact folio, which has the slug contact. WordPress will look for these files, in order:

  1. A custom page template, if you have assigned one to your page in its editing screen.
  2. A page template for that specific page using its slug – so in this case it would be page-contact.php.
  3. A general page template – page.php.
  4. The template file for all atypical posts of any post type, including pages – singular.php.
  5. The fallback alphabetize.php.

In the themes that I write, I always include a folio.php file. This reflects the fact that pages need to exist displayed differently from posts.

WordPress volition work through the hierarchy for archive files and single posts of any postal service type in a similar manner – it'due south worth familiarizing yourself with the hierarchy before yous start creating template files, so you lot know how they'll be used.

Single Template Files in Item

When displaying any kind of unmarried mail service, page or postal service of a custom postal service blazon, WordPress will utilise a template for a single post. That could be specific to the individual postal service, to the mail blazon, or just the atypical.php or alphabetize.php file.

The unmarried template file volition include the following:

  • A call to the header file using get_header().
  • A loop to output the mail service title and its content. For single posts, the file will oftentimes include metadata such as the appointment, categories etc.
  • A call to the sidebar file using get_sidebar(). If yous create a custom page template for full-width pages, this won't be included – or it will but the sidebar will exist styled differently so it appears beneath the content instead of next to it.
  • A call to the footer using get_footer().

Of grade, the specifics of what's included will vary according to the mail service type and the requirements of your theme. For example, the metadata will be different for different post types and you might include the featured image (as well known as the postal service thumbnail) in some postal service types but not others.

Here are the different kinds of single template you lot can create, in the order in which WordPress looks for them in the hierarchy:

  • For posts, WordPress volition look for a custom mail service template with the proper name $custom.php (i.e. any name y'all give it), if that template has been selected in the editing screen, thenunmarried-post.php, then single.php, followed byatypical.php and finallyindex.php.
  • For static pages, WordPress will use a custom page template, if you have assigned one to your page in its editing screen, followed by a page template for that specific page using its slug, thenpage.php,singular.php andindex.php.
  • For attachments (i.eastward images), WordPress will await for a $mimetype-$subtype.php file commencement, where $mimetype is the type of zipper (e.g. image or video) and $subtype is the file type (e.g. png or jpeg). This is followed by $subtype.php, then $mimetype.php, attachment.php and finally single.php, singular.php and alphabetize.php.
  • For custom postal service types, WordPress will look for a template for that postal service type with the slug of the specific mail service (unmarried-$postytpe-$slug.php), followed by single-$posttype.php, unmarried.php, singular.php and finally index.php.

Note: Information technology'south like shooting fish in a barrel to get confused between single.php and singular.php. The single.php file applies to all post blazon except pages, and is equivalent in the hierarchy to page.php. The atypical.php file applies to both posts and pages, and is the last option before index.php.

Archive Template Files in Item

Archive template files work slightly differently from single template files in that they're designed not for reading a post (or folio) but for providing a list of posts that people can then click through to.

Archive template files will still include the header, footer, and sidebar, merely the loop volition probably be different in i or more of a number of ways:

  • Yous might output the excerpt instead of the full content. Some themes output the full content on archive pages but in my view, this is less user-friendly than the excerpt.
  • If you lot're including the featured image, yous won't want to output information technology at full size just instead at thumbnail or medium size.
  • You may want to brandish different metadata.
  • Yous'll output the title of each post in a lower heading tag than for a unmarried post. For example, you might output the post championship on a unmarried page or post in a h1 tag but in an archive template, you lot put the primary page heading in a h1 tag and the post titles in a h2 tag.

You lot'll also need to add a title for the annal before the loop; how you lot do this will depend on the blazon of archive yous're creating.

Here's what WordPress looks for when displaying archive pages:

  • Author archives: author-$nicename.php, where $nicename is the name the writer uses on the site; then author-$ID.php, where $ID is the author'south ID, followed by writer.php, archive.php and finally index.php.
  • Category archives: category-$slug.php, using the slug for that category, then category-$id.php, followed by category.php, archive.php and index.php.
  • Tag archives work in the aforementioned style as category athenaeum, substituting tag for category in the filenames.
  • Custom post type archives: archive-$posttype.php, where $posttype is the ID of the postal service type used when registering it. This is followed by archive.php and index.php.
  • Date archives: date.php, then archive.php, then index.php.
  • Custom taxonomy athenaeum: taxonomy-$taxonomy-$term.php, where $taxonomy is the slug for the taxonomy and $term is the slug for the currently displayed term in that taxonomy, followed by taxonomy-$taxonomy.php, then taxonomy.php, archive.php and index.php. And so if you had a product_type taxonomy and a widget term in it, the files WordPress would look for are taxonomy-product_type-widget.php, thentaxonomy-product_type.php, then taxonomy.php, archive.php and index.php.

Note that all archive files will default to annal.php before alphabetize.php. so information technology's a skillful idea to add this file to your theme at the very least.

The Home Folio and the Main Blog Page

If the forepart page is a static folio or a list of your posts, then the beginning template file in the hierarchy is front-page.php. I often utilise this to display the contents of a static page using a standard loop, then add one or more than instances of WP_Query to output contempo posts too.

If your home page is a static page and there is no front-page.php, so WordPress volition use a page template, in the same way as information technology would for any normal page.

If your home page is also your web log page, WordPress will wait for home.php next, followed by index.php.

If your home page is a static page, WordPress volition apply home.php for your main weblog page, followed by index.php. Note that none of your other annal template files are used for the main weblog page, so don't expect your archive.php file to be used in that location.

Special Templates in Detail

The 404 folio has its own template file designed to exist displayed when in that location's an error. If you don't create ane, WordPress will fall dorsum to the index.php file, so you'll need to include an error message in that, within the is_404() conditional tag.

It's a expert idea to create a 404.php template file so you can include extra content designed to help people who have hit an error, such as a search box or a listing of your site's almost recent posts. This means people have somewhere to get from the error page and you're less likely to lose them.

Another special template is the search.php file. This includes a loop outputting the results of the search plus introductory text explaining what'south listed. You could also add actress content such as a list of your most recent posts in example the search doesn't produce any results.

Template Parts in Detail

Template parts assistance you make your code more than efficient. By separating out parts of the code that are included in multiple template files, you merely need to write the code one time.

The most unremarkably used template parts are:

  • header.php – for everything from the opening of the page to the end of the page header. This volition include the head section, the opening of the trunk section, the header element and your chief navigation.
  • sidebar.php – for the sidebar widgets.
  • footer.php – for the page footer (the footer element), the wp_footer claw and the endmost </body> tag.
  • loop.php – for the loop. Coding this one time means y'all can use the same loop in multiple template files, and you only have to edit it once if that becomes necessary. You can create multiple versions of the loop for dissimilar kinds of content and call them from the relevant template files, such equally one for unmarried posts, one for pages and 1 for archives.

The header, sidebar and footer files each have their own template tag you utilize to include them in your template files: get_header(), get_sidebar() and get_footer(). For the loop, utilise get_template_part( 'loop' ) to include a file called loop.php.

Understanding Template Files Will Help You Build Themes

This postal service was designed to help you understand theme template files and how they work. You now know what they consist of, what they're designed to display and how WordPress decides which one to use. This will assistance y'all determine which files to add to your own themes and code them effectively.

Tags:

My Theme Does Not Have An Index.php Template?,

Source: https://wpmudev.com/blog/the-ultimate-guide-to-theme-template-files-in-wordpress/

Posted by: butlerwheirs.blogspot.com

0 Response to "My Theme Does Not Have An Index.php Template?"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel