Solution
As I alluded to in the comments on your earlier question, the Codex entry for conditional tags details,
You can only use conditional query tags after the posts_selection
action hook in WordPress (the wp
action hook is the first one through
which you can use these conditionals). For themes, this means the conditional tag will never work properly if you are using it in the body of functions.php
, i.e. outside of a function.
That in mind, you need to refer to the Action Reference to find a hook after posts_selection
suitable for your purposes, then use conditionals in a function attached to that hook. I recommend brushing up on the WordPress Plugin API to get a better understanding of how hooks function. Smashing Magazine also provides a decent less-technical intro to hooks, though it is slightly outdated.
In functions.php
:
add_action( 'wp', 'wpse177227_load_shortcodes' );
function wpse177227_load_shortcodes() {
if( ! is_page_template( 'template-flat.php' ) )
require_once( 'path/to/shortcodes.php' );
}
Mechanism
I think it may be beneficial to break down this code's mechanism somewhat:
- WordPress loads the
functions.php
file
function wpse177227_load_shortcodes() {}
is a function definition, so PHP simply takes note of the fact that a chunk of code identified by 'wpse177227_load_shortcodes' is available for future reference. The function's body is not evaluated at this point.
add_action();
is a function call - the function's logic is evaluated when the call is evaluated - since the call to add_action()
is not contained in a function, this means that it is executed as soon as WordPress loads the functions.php
file. Called with the arguments wp
and wpse177227_load_shortcodes
, this instructs WordPress to try and evaluate a function named wpse177227_load_shortcodes()
if and when the action wp
occurs.
- WordPress calls
do_action( 'after_setup_theme' )
, signifying that the active theme's functions.php
file has been evaluated and that any functions contained within the file is are now available for use.
- WordPress continues it's loading routine, calling
do_action('{action_name}')
at specific milestones to provide points at which other code may be evaluated. The order that WordPress executes action hooks is roughly equivelant to the order in which they are listed in the Action Reference for most requests.
- After determining and acquiring the data that's needed to produce a response and setting up the WP object, WordPress calls
do_action( 'wp' );
, signifying that the WordPress environment is established (i.e. all plugins and themes loaded, the WP object is aware of what the end-user is asking for and contains the data needed to build a response).
- WordPress sees that a function named 'wpse177227_load_shortcodes' was previously attached to the
wp
action (via the add_action()
call that was evaluated when functions.php
was loaded) and attempts to call wpse177227_load_shortcodes();
- Since PHP previously encountered the function definition for 'wpse177227_load_shortcodes' when
functions.php
was loaded, the call succeeds and the function's body is evaluated.
- WordPress encounters the conditional tag
is_page_template()
. Since this code was not evaluated until the wp
action, WordPress is aware of the nature of the content being processed and can properly answer conditional questions regarding the content.
That's something of a dense (but hopefully not overly-technical) overview, but I hope it might aide you in grasping the nature of hooks. In general, they allow you to defer execution of your code to a specific moment in WordPress's execution. The trick is using the Codex, Action Reference, and other resources to determine the proper hook for your needs.