How to optimize this function
-
Hi there, I was wondering if someone knows how I can optimize, and hopefully dramatically shrink the function below.
It is used within the function.php of a custom template and it’s used to retrieved custom designs for each of the individual sections of the blog/site. The problem is that it’s immensely long and it seems to cause some little CPU overload on the server.
Anybody would know how to optimize it? Thanks in advance!
/******************************************************************** loading a custom single post template ********************************************************************/ function get_custom_post() { global $post, $chic; $get_post_cat = wp_get_post_categories($post->ID); foreach($get_post_cat as $catid ) { $cat = get_category($catid); $the_cat = get_cat_name($cat->cat_ID); } // videos $video_sustainability = get_cat_name($chic['video_sustainability_id']); $video_art_and_design = get_cat_name($chic['video_art_design_id']); $video_conscious_living = get_cat_name($chic['video_conscious_living_id']); $video_wellbeing = get_cat_name($chic['video_wellbeing_id']); // Wellbeing $in_the_balance = get_cat_name($chic['in_the_balance_id']); $intimacy = get_cat_name($chic['intimacy_id']); $social_trends = get_cat_name($chic['social_trends_id']); $sustainable_life = get_cat_name($chic['sustainable_life_id']); $wellness_notes = get_cat_name($chic['wellness_notes_id']); // Travel $dream_destinations = get_cat_name($chic['dream_destinations_id']); $globe_trotter = get_cat_name($chic['globe_trotter_id']); $haute_hotels = get_cat_name($chic['haute_hotels_id']); $spas = get_cat_name($chic['spas_id']); $wanderlust = get_cat_name($chic['wanderlust_id']); // Culture $art_and_design = get_cat_name($chic['art_and_design_id']); $books = get_cat_name($chic['books_id']); $cuisine = get_cat_name($chic['cuisine_id']); $on_the_agenda = get_cat_name($chic['on_the_agenda_id']); $photography = get_cat_name($chic['photography_id']); $lifestyle_notes = get_cat_name($chic['lifestyle_notes_id']); // Fashion $style_mantra = get_cat_name($chic['style_mantra_id']); $style_notes = get_cat_name($chic['style_notes_id']); $street_style = get_cat_name($chic['street_style_id']); $trendwatch = get_cat_name($chic['trendwatch_id']); $stylist_on_demand = get_cat_name($chic['stylist_on_demand_id']); $wish_list = get_cat_name($chic['wish_list_id']); // Beauty $must_have_products = get_cat_name($chic['must_have_products_id']); $beauty_trends = get_cat_name($chic['beauty_trends_id']); $dream_destinations = get_cat_name($chic['dream_destinations_id']); $scentsual = get_cat_name($chic['scentsual_id']); $top_tips = get_cat_name($chic['top_tips_id']); $beaute_notes = get_cat_name($chic['beaute_notes_id']); // Celebs $interviews = get_cat_name($chic['interviews_id']); $chicest_celebs = get_cat_name($chic['chicest_celebs_id']); $get_the_look = get_cat_name($chic['get_the_look_id']); $the_good_celebritan = get_cat_name($chic['the_good_celebritan_id']); $the_red_carpet = get_cat_name($chic['the_red_carpet_id']); $celebrity_notes = get_cat_name($chic['celebrity_notes_id']); // Magazine $back_issues = get_cat_name($chic['back_issues_id']); $current_issue = get_cat_name($chic['current_issue_id']); // Competitions $current_competitions = get_cat_name($chic['current_competitions_id']); $past_competitions = get_cat_name($chic['past_competitions_id']); // re-set variable for-each // Videos $article_video_sustainability = '/^'. $video_sustainability .'/'; $article_video_art_and_design = '/^'. $video_art_and_design .'/'; $article_conscious_living = '/^'. $video_conscious_living .'/'; $article_video_wellbeing = '/^'. $video_wellbeing .'/'; // Wellbeing $article_in_the_balance = '/^'. $in_the_balance .'/'; $article_intimacy = '/^'. $intimacy .'/'; $article_social_trends = '/^'. $social_trends .'/'; $article_sustainable_life = '/^'. $sustainable_life .'/'; $article_wellness_notes = '/^'. $wellness_notes .'/'; // Travel $gallery_dream_destinations = '/^'. $dream_destinations .'/'; $blog_globe_trotter = '/^'. $globe_trotter .'/'; $gallery_haute_hotels = '/^'. $haute_hotels .'/'; $gallery_spas = '/^'. $spas .'/'; $article_wanderlust = '/^'. $wanderlust .'/'; // Culture $gallery_art_and_design = '/^'. $art_and_design .'/'; $article_books = '/^'. $books .'/'; $article_cuisine = '/^'. $cuisine .'/'; $article_on_the_agenda = '/^'. $on_the_agenda .'/'; $gallery_photography = '/^'. $photography .'/'; $article_lifestyle_notes = '/^'. $lifestyle_notes .'/'; // Fashion $gallery_style_mantra = '/^'. $style_mantra .'/'; $article_style_notes = '/^'. $style_notes .'/'; $blog_street_style = '/^'. $street_style .'/'; $gallery_trendwatch = '/^'. $trendwatch .'/'; $gallery_stylist_on_demand = '/^'. $stylist_on_demand .'/'; $gallery_wish_list = '/^'. $wish_list .'/'; // Beauty $gallery_must_have_products = '/^'. $must_have_products .'/'; $gallery_beauty_trends = '/^'. $beauty_trends .'/'; $gallery_scentsual = '/^'. $scentsual .'/'; $gallery_top_tips = '/^'. $top_tips .'/'; $article_beaute_notes = '/^'. $beaute_notes .'/'; // Celebs $article_interviews = '/^'. $interviews .'/'; $gallery_chicest_celebs = '/^'. $chicest_celebs .'/'; $gallery_get_the_look = '/^'. $get_the_look .'/'; $article_the_good_celebritan = '/^'. $the_good_celebritan .'/'; $gallery_the_red_carpet = '/^'. $the_red_carpet .'/'; $article_celebrity_notes = '/^'. $celebrity_notes .'/'; // Magazine $article_back_issues = '/^'. $back_issues .'/'; $article_current_issue = '/^'. $current_issue .'/'; // Competitions $article_current_competitions = '/^'. $current_competitions .'/'; $article_past_competitions = '/^'. $past_competitions .'/'; // IF condition Videos if (preg_match($article_video_sustainability, $the_cat)) { include TEMPLATEPATH. '/article_video_sustainability.php'; } elseif (preg_match($article_video_art_and_design, $the_cat)) { include TEMPLATEPATH. '/article_video_art_design.php'; } elseif (preg_match($article_conscious_living, $the_cat)) { include TEMPLATEPATH. '/article_video_conscious_living.php'; } elseif (preg_match($article_video_wellbeing, $the_cat)) { include TEMPLATEPATH. '/article_video_wellbeing.php'; } // Wellbeing elseif (preg_match($article_in_the_balance, $the_cat)) { include TEMPLATEPATH. '/article_in_the_balance.php'; } elseif (preg_match($article_intimacy, $the_cat)) { include TEMPLATEPATH. '/article_intimacy.php'; } elseif (preg_match($article_social_trends, $the_cat)) { include TEMPLATEPATH. '/article_social_trends.php'; } elseif (preg_match($article_sustainable_life, $the_cat)) { include TEMPLATEPATH. '/article_sustainable_life.php'; } elseif (preg_match($article_wellness_notes, $the_cat)) { include TEMPLATEPATH. '/article_wellness_notes.php'; } // Travel elseif (preg_match($gallery_dream_destinations, $the_cat)) { include TEMPLATEPATH. '/gallery_dream_destinations.php'; } elseif (preg_match($blog_globe_trotter, $the_cat)) { include TEMPLATEPATH. '/blog_globe_trotter.php'; } elseif (preg_match($gallery_haute_hotels, $the_cat)) { include TEMPLATEPATH. '/gallery_haute_hotels.php'; } elseif (preg_match($gallery_spas, $the_cat)) { include TEMPLATEPATH. '/gallery_spas.php'; } elseif (preg_match($article_wanderlust, $the_cat)) { include TEMPLATEPATH. '/article_wanderlust.php'; } // Culture elseif (preg_match($gallery_art_and_design, $the_cat)) { include TEMPLATEPATH. '/gallery_art_and_design.php'; } elseif (preg_match($article_books, $the_cat)) { include TEMPLATEPATH. '/article_books.php'; } elseif (preg_match($article_cuisine, $the_cat)) { include TEMPLATEPATH. '/article_cuisine.php'; } elseif (preg_match($article_on_the_agenda, $the_cat)) { include TEMPLATEPATH. '/article_on_the_agenda.php'; } elseif (preg_match($gallery_photography, $the_cat)) { include TEMPLATEPATH. '/gallery_photography.php'; } elseif (preg_match($article_lifestyle_notes, $the_cat)) { include TEMPLATEPATH. '/article_lifestyle_notes.php'; } // Fashion elseif (preg_match($gallery_style_mantra, $the_cat)) { include TEMPLATEPATH. '/gallery_style_mantra.php'; } elseif (preg_match($article_style_notes, $the_cat)) { include TEMPLATEPATH. '/article_style_notes.php'; } elseif (preg_match($blog_street_style, $the_cat)) { include TEMPLATEPATH. '/blog_street_style.php'; } elseif (preg_match($gallery_trendwatch, $the_cat)) { include TEMPLATEPATH. '/gallery_trendwatch.php'; } elseif (preg_match($gallery_stylist_on_demand, $the_cat)) { include TEMPLATEPATH. '/gallery_stylist_on_demand.php'; } elseif (preg_match($gallery_wish_list, $the_cat)) { include TEMPLATEPATH. '/gallery_wish_list.php'; } // Beauty elseif (preg_match($gallery_must_have_products, $the_cat)) { include TEMPLATEPATH. '/gallery_must_have_products.php'; } elseif (preg_match($gallery_beauty_trends, $the_cat)) { include TEMPLATEPATH. '/gallery_beauty_trends.php'; } elseif (preg_match($gallery_scentsual, $the_cat)) { include TEMPLATEPATH. '/gallery_scentsual.php'; } elseif (preg_match($gallery_top_tips, $the_cat)) { include TEMPLATEPATH. '/gallery_top_tips.php'; } elseif (preg_match($article_beaute_notes, $the_cat)) { include TEMPLATEPATH. '/article_beaute_notes.php'; } // Celebs elseif (preg_match($article_interviews, $the_cat)) { include TEMPLATEPATH. '/article_interviews.php'; } elseif (preg_match($gallery_chicest_celebs, $the_cat)) { include TEMPLATEPATH. '/gallery_chichest_celebs.php'; } elseif (preg_match($gallery_get_the_look, $the_cat)) { include TEMPLATEPATH. '/gallery_get_the_look.php'; } elseif (preg_match($article_the_good_celebritan, $the_cat)) { include TEMPLATEPATH. '/article_the_good_celebritan.php'; } elseif (preg_match($gallery_the_red_carpet, $the_cat)) { include TEMPLATEPATH. '/gallery_the_red_carpet.php'; } elseif (preg_match($article_celebrity_notes, $the_cat)) { include TEMPLATEPATH. '/article_celebrity_notes.php'; } // Magazine elseif (preg_match($article_back_issues, $the_cat)) { include TEMPLATEPATH. '/article_magazine.php'; } elseif (preg_match($article_current_issue, $the_cat)) { include TEMPLATEPATH. '/article_magazine.php'; } // Competitions elseif (preg_match($article_current_competitions, $the_cat)) { include TEMPLATEPATH. '/article_competitions.php'; } elseif (preg_match($article_past_competitions, $the_cat)) { include TEMPLATEPATH. '/article_competitions.php'; } // Conclude else { include TEMPLATEPATH. '/article_general.php'; } }
- The topic ‘How to optimize this function’ is closed to new replies.