Support » Requests and Feedback » Separate theme files for posts

  • I’m not sure if this is something that has come up before since I’m not sure what keywords to search on. A quick search on Google didn’t show anything, but if there is already a thread for this I’d appreciate knowing about it.

    Anyway, I would like to see theme files become more modular. Right now the header, footer and sidebars are usually separate files but the main theme file is responsible to call them. And it is more of a convention rather than a requirement.

    It would be nice if the styling for individual posts and excerpts could be separated out so that they can be reused. This would help in keeping the theme files up to date when a change is made to the markup — it is only in 1 file rather than in each file that has a loop.

    It could even be abstracted a couple levels deep, the main file for a page, calls separate files to build the header, content, sidebar and footer. The content file would then call in the post or excerpt file (depending on which is needed) as part of its loop.

    This would also allow a plugin to track down the standard formatting for a post or loop used in the theme and use them if required.

    I think that Drupal and XOOPs use a model like this. The biggest issue that I see with moving this direction would be backwards compatibility for themes. Any code or plugin written to take advantage of this structure would not work in an older theme.

    Anyway, what do you guys and gals think of this idea?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator Ipstenu (Mika Epstein)


    🏳️‍🌈 Halfelf Rogue & Plugin Review Team Rep

    I think that you should look into how the template hierarchy works.

    You’re probably not going to get a lot of traction on something that re-writes everything that went into all that. It would be very hard to do that and keep every single theme in the themes repo still working.

    What I’m envisioning (and it is at a basic level at this point) is an extension of the template hierarchy.

    For example, there could be a loop.php and get_loop() function would call it. There could even be overrides like archive-loop.php which is called when get_loop() is called within archive*.php files, etc.

    Then there would be loop-post.php which would be called inside the loop with get_loop_post().

    You could expand get_header() to load related headers using the same hierarchy.

    As you mention, it would be a major change and might break a lot of themes, but it would make life easier especially when building child themes and plugins that work with themes.



    Forum Moderator

    Have you looked at the Twenty Ten & Twenty Eleven themes? Or checked out get_template_part? I think the core devs are ahead of you. 😉

    Thanks esmi. It does seem that they are heading in that direction. This would make what I’m suggesting possible for a given theme developer, but it does lack the standardization I’m hoping for.

    It would be nice to see every theme use a separate file for the loop and another for each individual post within the loop. The naming convention should be standardized so that a plugin could work with it regardless of which theme was running.

    It’s probably not possible for this to happen, but it would be nice.



    Forum Moderator

    I don’t think you are ever going to get that level of standardisation amongst theme devs. Without trying to sound “precious”, it’s going to severely cramp overall creativity – especially with some of the highly specialised, niche, themes.

    While I think you are probably right, I think it would be possible to have the flexibility for those that want to go beyond the standard loop.

    After looking at the documentation for get_template_part() and the example they use for the loop I think it wouldn’t be much of a stretch to expand that. Have the ‘name’ argument default to the name of the current template piece rather than nothing. The same thing could be applied to the ‘name’ arg for get_header(), et. al.

    Then add a get_loop() (which would be as simple as calling get_template_part(‘loop’, $arg)) and get_post() (probably a name conflict there so a different naming convention might be needed).

    Retrofitting an existing theme would be relatively easy as you could just create a couple new files by pulling out the loop(s) from index.php, etc.

    Those who need to go outside of a typical main loop would still be free to do their own thing, but plugins based on a standardized loop wouldn’t work with them. They could still make use of get_loop() when it suits their needs.

    I’ve been trying to work on a plugin that adds an extra loop (e.g. “Featured Posts”) to any theme on the home page, so having this type of standardization would make it easy to adopt the styling of the theme for the loop by simply calling get_loop().

    If someone wanted to add a single post somewhere, the get_post() call would be valuable.

    Anyway, thanks esmi and Ipstenu for your comments. It feels good to have some feedback as I think this through. While I agree that this is not likely to happen, I guy can dream can’t he? 8=)

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Separate theme files for posts’ is closed to new replies.