Separating logic and presentation in wordpress Recently, I've been thinking about better ways to make my WP themes more maintainable. What I've tried on one of my last projects is to separate out the presentation and logic as much as I can. First, I created a function with the loop in it and has all of the logic. It returns all of the data into an array. In the template page, I call the function, loop through the array, and output the HTML.

At the most basic level, this is what it looks like. In functions.php (or a plugin):

function get_data(){
    if (have_posts()) : 
        while (have_posts()) : the_post();
            $results['title'] = get_the_title();
            $results['field'] = get_field('field');
        endwhile;
    endif;
    return $results;
}


In the template file:

query_posts( 'posts_per_page=5' );
$results = get_data();
foreach ($results as $result) :
    &lt;section&gt;
        &lt;h1&gt;echo $result['title'];&lt;/h1&gt;
        echo $result['field'];
    &lt;/section&gt;
endforeach;


Is this a good practice? From what I can see, it's a good solution but I haven't seen any other developers use it. Is there something better that other people are doing?
Actually, there is something better, and that is using templates. Yes, it might be "radical" especially when you see no other developer near you use it. And yes, even my college instructors and friends don't know what templates are and still stuff logic with the presentation so I know how it feels as well.

I suggest you take a look at Mustache for starters. Basically, your templates will be logicless. The template will only contain a bit of custom markup. What your logic will only need to do is load the data, load the template, use the render function and it returns rendered markup, complete with the data.