Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

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
share|improve this answer
    
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? –  bigp Nov 1 '11 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. –  Chip Bennett Nov 1 '11 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. –  Chip Bennett Nov 1 '11 at 15:33
    
Alright, will ask it separately. I'll post the link in another comment shortly ;) –  bigp Nov 1 '11 at 15:35
1  
Here it is: Question about wp_title() wordpress.stackexchange.com/q/32622/9710 –  bigp Nov 1 '11 at 15:44
add comment

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.

share|improve this answer
    
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.) –  bigp Nov 1 '11 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 Nov 5 '11 at 20:28
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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