0

Recently I've been developing sites for WordPress that are both increasingly complex but also have much higher rates of traffic. I remember when I was learning the ropes from another about PHP, that in cases of optimisation, "cases" are a better option than "IFs", as they put less load on the server.

I've successfully been able to use "cases" in some functions, but when it comes to use in files like single.php, then I've failed miserably.

So I thought I'd push it out to the community to find out how simple IF logic inside WordPress loops, can be optimised with "cases".

My code below basically checks whether the content being loaded is for one of six custom post types, and depending on which post type it is, then different parameters are fed into the custom functions.

if (is_singular('news')) {
    terms_by_order('news_category', array(3623));
    if( false != get_the_term_list( $post->ID, 'news_column' ) ) {
        echo ' | ' . get_the_term_list($post->ID,'news_column', ' ', ' ', '' );
    }
} 

/* <!-- if sport --> */
if (is_singular('sports')) {
    terms_by_order('sports_category', array(3623));
    if( false != get_the_term_list( $post->ID, 'sports_column' ) ) {
       echo ' | ' . get_the_term_list($post->ID,'sports_column', ' ', ' ', '' );
    }
} 

/* <!-- if opinion --> */
if (is_singular('opinion')) {
    terms_by_order('opinion_category', array(3623));
    if( false != get_the_term_list( $post->ID, 'opinion_column' ) ) {
        echo ' | ' . get_the_term_list($post->ID,'opinion_column', ' ', ' ', '' );
    }
} 

/* <!-- if life --> */
if (is_singular('life')) {
    terms_by_order('life_category', array(3623));
    if( false != get_the_term_list( $post->ID, 'life_column' ) ) {
        echo ' | ' . get_the_term_list($post->ID,'life_column', ' ', ' ', '' );
    }
} 

/* <!-- if culture --> */
if (is_singular('culture')) {
    terms_by_order('culture_category', array(3623));
    if( false != get_the_term_list( $post->ID, 'culture_column' ) ) {
        echo ' | ' . get_the_term_list($post->ID,'culture_column', ' ', ' ', '' );
    }
}


/* <!-- if community --> */
if (is_singular('community')) {
    terms_by_order('community_category', array(3623));
    if( false != get_the_term_list( $post->ID, 'community_column' ) ) {
        echo ' | ' . get_the_term_list($post->ID,'community_column', ' ', ' ', '' );
    }
} 
2
  • This is a PHP question that isn't specific to WordPress - should be transferred to SO.
    – anu
    Commented Oct 29, 2012 at 9:57
  • A fair point, I wasn't 100% sure how the distinct for a question like this was made between the two sites.
    – Ashkas
    Commented Oct 29, 2012 at 10:26

2 Answers 2

3

If you're using it on single templates, the easiest & most efficient way would be to switch based on the value of get_post_type().

switch(get_post_type()) {
    case 'news' : 
        // some statement for news type
    break;
    case 'sports' :
        // some statement for sports type
    break;
    default:
        // something to do when it's none of the above
    break;
}

In my opinion a foreach here would be an overkill(specially if there are a lot of post types like in the question)

1

Instead of writing the code for every case, why not just extract the post type specific information like the category-name or the column-name (aka the "data") in an array and separate it from the logic (aka the "code"):

// here's the data - the key is the post type, the value is an array 
// with the column and category name (or any other post-type specific
// information you have).
$my_types = array(
    'news' => array(
        'category' => 'news_category',
        'column' => 'news_column',
        // ... other post type specific parameters
    ),
    'sports' => array(
        'category' => 'sports_category',
        'column' => 'sports_column',
    ),
    'opinion' => array(
        'category' => 'opinion_category',
        'column' => 'opinion_column',
    ),
    'life' => array(
        'category' => 'life_category',
        'column' => 'life_column',
    ),
    'culture' => array(
        'category' => 'culture_category',
        'column' => 'culture_column',
    ),
    'community' => array(
        'category' => 'community_category',
        'column' => 'community_column',
    ),
    // add more post types here
);

$resultlist = array();

// and here's the actual logic which is executed
// for every post type in the array.
foreach($my_types as $typename => $type) {
    if(is_singular($typename)) {
        terms_by_order($type['category'], array(3623));

        // just call "get_the_term_list" once
        $list = get_the_term_list($post->ID, $type['column'], ' ', ' ', '');

        // check if it is a valid list - get_the_term_list could
        // return false or a WP_Error.
        if(!($list === false || is_wp_error($list))) {
            // instead of directly "echo" the list, collect it in an array
            $resultlist[] = $list;
        }
    }
}

// echo the list with the separator - that way you don't have
// to manually track whether you have to echo a separator or not
echo implode(' | ', $resultlist);

You can make it even shorter if you are sure that the category name or column name are always derived from the post type in the same way (like bla => bla_category => bla_column).

1
  • Ah brilliant and more stream-lined that I was originally thinking. The concept of remembering "key" + "value" is where I went off course with my attempts. Going to bookmark this :)
    – Ashkas
    Commented Oct 29, 2012 at 10:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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