WordPress.org

Forums

DRY in WordPress Templates (5 posts)

  1. danharper
    Member
    Posted 5 years ago #

    When displaying posts in your WordPress theme, you usually have the same chunk of code in multiple places throughout your theme, and it usually never changes.
    For example, you typically have the same code in index.php, archive.php, search.php etc. and even the single.php page contains very similar code.

    That doesn’t make any sense at all. A better approach is to include that code in one file, and reference it from there: Don’t Repeat Yourself.

    I cover this in this short screencast.

    I realised how horrible WP theme code can be after using Ruby on Rails for a month. Rails encourages the DRY programming philosophy and I believe WordPress should take this direction also.

    I'd love to hear the community's opinion on this. Maybe we can revolutionise how templates are coded?

  2. Samuel B
    moderator
    Posted 5 years ago #

    A better approach is to include that code in one file, and reference it from there

    oh yea - I want to wade through one giant file to modify stuff - not
    wp uses a simple hierarchy that is easy to work with
    Ruby on Rails is a huge pain in the butt
    my .02

  3. yasanto
    Member
    Posted 5 years ago #

    How to install the wordpress in vista ? I want to manage my blog in localhost but failed. I used xampp to query database

    Help me, how the step and what the software support it

    Regard

    santo

  4. Samuel B
    moderator
    Posted 5 years ago #

  5. danharper
    Member
    Posted 5 years ago #

    oh yea - I want to wade through one giant file to modify stuff - not
    wp uses a simple hierarchy that is easy to work with
    Ruby on Rails is a huge pain in the butt
    my .02

    How so? This file only contains the code for displaying a post. Everything else is handled inside their relevant files.

    Obviously, if your posts are drastically different between pages (eg. how you display a post in index is completely different to how it looks in archive) then it makes sense not to do it this way.
    But I'd guess the vast majority of templates contain the same post layout throughout.

    The only normal differences would be:

    • Content vs Excerpt
    • Linked Title vs Plain Text
    • Displaying category & comment count (if using the partial for a page.)

    and all these can be handled with a simple 'if is_single', 'if is_post'.

    Personally, I'd much rather have one file containing my post layout than have the exact same code in a bunch of files.

Topic Closed

This topic has been closed to new replies.

About this Topic