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', ' ', ' ', '' );
}
}