loliki
loliki

Reputation: 957

Include a php file from functions.php only to homepage

I'm re-developing a wordpress theme using sage them framework. I have this array in functions.php

$sage_includes = [
  'lib/assets.php',    // Scripts and stylesheets
  'lib/theme_options.php',    // Theme Options
  'lib/setup.php',     // Theme setup
  'lib/titles.php',    // Page titles
  'lib/wrapper.php',   // Theme wrapper class
  'lib/home_shortcodes.php', // Homeapage Shortcodes
  'lib/shortcodes.php' // Old Theme Shortcodes

];

which is loaded below

foreach ($sage_includes as $file) {
  if (!$filepath = locate_template($file)) {
    trigger_error(sprintf(__('Error locating %s for inclusion', 'sage'), $file), E_USER_ERROR);
  }

  require_once $filepath;
}
unset($file, $filepath);

Now I have home_shortcodes.php and shortcodes.php, I want the home_shortcodes.php to be loaded only on my front page / homepage how can I achieve something like that?

Upvotes: 0

Views: 89

Answers (1)

Ahmed Ginani
Ahmed Ginani

Reputation: 6650

Just you need to add the extra condition to check if the current page is homepage or front page.

function load_files(){
    if(is_home() || is_front_page()){
       if($file == 'lib/home_shortcodes.php'){
         $filepath = locate_template($file);
         require_once $filepath;
       }
       else{
         $filepath = locate_template($file);
         require_once $filepath;
       }
    }
}
add_action('after_theme_setup','load_files');

Upvotes: 1

Related Questions