0

I've started to develop my own theme in the past couple weeks (and extracting WP functionality here and there in the past year), and I'm finding the structure of the WP templates awful redundant.

Although that yes, technically, the blog starts somewhere higher above to load the WP functionality, the main WP_Query object, and some global variable preparation - I don't understand why every themes that I've stumbled upon so far splits the template files as:

  • 404.php;
  • archive.php;
  • index.php;
  • single.php;
  • page.php;
  • search.php;
  • ... etc;

Those files basically duplicate a lot of PHP code to fetch the Header, Sidebar and Footer.

Wouldn't it be cleaner to have one (1) index.php file that includes the Header, Sidebar, Footer... and within the index.php file - you could decide how to show the content?

I'm not sure I understand the need to have separate files.

If you need to change the title in your <head> tag, why not use the power of output-buffering (ob_start() and ob_end_clean())to get the posts first / the page / the single post / the search results... Is there performance issues using this technique?

Could I run into problems if I wish to make my theme mobile-friendly, and readable by RSS feed readers?

-------------- EDIT:

I bring up this question because there is PHP methods that exists to detect in which section the user is visiting (ex: is_single(), is_page(), is_archive(), is_home(), etc...) and I would like to know if those could be used to achieve a central point for my index.php file.

3 Answers 3

2

Theme template files are organized in this way because of the WordPress Template Hierarchy. Since all primary template files eventually fallback to index.php, it is certainly possible to use only the index.php primary template file. There are advantages and disadvantages to using either method.

Generally speaking, the usefulness/efficiency of defining template files is inversely proportional to the contextual complexity of the Theme. If your Theme output does not changed based on context, as defined in the Template Hierarchy, then using only an index.php file makes perfect sense. However, trying to implement more-and-more complex, contextually specific output becomes increasingly difficult using only query-based conditional template tags (e.g. is_single(), is_archive(), is_category(), is_404(), etc.) inside of index.php.

However, this is a bad idea:

If you need to change the title in your <head> tag, why not use the power of output-buffering (ob_start() and ob_end_clean())to get the posts first / the page / the single post / the search results... Is there performance issues using this technique?

There is no need for output buffering. You should use one of the following methods:

  1. The the_title filter hook to change wp_title() output contextually
  2. Pass a contextually defined variable directly to the wp_title() template tag
5
  • Thank you very much for this answer. So in calling wp_title(), would I need to create a title.php file that handles the various title cases, or does wp_title() handle that already? Commented Nov 1, 2011 at 14:56
  • "So in calling wp_title(), would I need to create a title.php file that handles the various title cases, or does wp_title() handle that already?" - post this as a separate question, and I'll be happy to write up an answer. Commented Nov 1, 2011 at 14:58
  • To clarify: the answer to this second question is involved enough that it warrants its own question; it will make the question and answer easier for future users to find. Commented Nov 1, 2011 at 15:33
  • Alright, will ask it separately. I'll post the link in another comment shortly ;) Commented Nov 1, 2011 at 15:35
  • 1
    Here it is: Question about wp_title() wordpress.stackexchange.com/q/32622/9710 Commented Nov 1, 2011 at 15:44
1

You can actually use a single index.php to create your WordPress theme. all you need is a style.css and index.php (along wit footer and header) it is all up to theme developer. I did that for old style classic bloc design a lot, but today, while designing complex CMS and Magazine themes; using different files according WordPress Template Hierarchy makes things a lot simpler.

The customization becomes way more easier for newbies who are not familiar with PHP functions but good at CSS/XHTML.

2
  • Thanks, makes sense. I assume your old style classic blog design didn't change much in each available contexts right? (single, posts, search, page, home, etc.) Commented Nov 1, 2011 at 14:54
  • Exactly, the layout for archive search and page were all same. I had simple functions under conditional logics to display specific pages for search, category, page etc...
    – Mehmet
    Commented Nov 5, 2011 at 20:28
0

WordPress themes are typically organized as multiple PHP files to allow for greater flexibility and modularity. This allows developers to create different templates for different types of pages or content within the theme. For example, a theme might have separate templates for the home page, single post page, and page for displaying a list of blog posts.

Using separate templates allows developers to customize the layout and design of each type of page independently, rather than having to create a single monolithic template that handles all types of pages. This can make it easier to modify the theme and customize it to fit the needs of a specific site.

The main entry point for a WordPress theme is typically the index.php file, which acts as a fallback template that is used if no other more specific template is available. This means that the index.php file can be used to display any type of page for which there is no other template defined.

Overall, the use of multiple template files allows for greater flexibility and customization in WordPress themes and helps to make it easier for developers to create unique and customized designs for their sites.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.