Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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) :
    <section>
        <h1>echo $result['title'];</h1>
        echo $result['field'];
    </section>
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?

share|improve this question
add comment

2 Answers 2

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.

share|improve this answer
add comment

Your get_data() function is broken. You are overwriting $results['title'] and $results['field'] on each iteration, instead of appending it to $results array. Moreover, if no posts existed to loop over, you'd get an "undefined variable $results" notice. Let's fix both issues:

function get_data(){
    $results = array(); // <- Initialize array to prevent "undefined variable" notices
    if (have_posts()) : 
        while (have_posts()) : the_post();
            $results[] = array( // <- Append a new result set to the results
                'title' => get_the_title(),
                'field' => get_field('field'),
            );
        endwhile;
    endif;
    return $results;
}

About the code in your template file, it'd be better not to use query_posts() at all. Use the pre_get_posts hook to alter the main query if needed. Please, see this question over at WordPress Answers.

share|improve this answer
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.