Note: This code is run on the setup_theme
hook so WordPress query functions cannot be used. However, ThemeSwitcher Pro provides some alternatives, such as:
tsp_get_post_id()
: To get the post id.
tsp_get_taxonomy_slug()
: To get the taxonomy slug.
tsp_is_taxonomy( $taxonomy_slug )
: Check if the request is from a taxonomy.
tsp_get_term()
: To get the current term name.
tsp_is_front_page()
: Check if the request is from a front page.
Some of the code examples:
NOTE: In order for code snippets to run reliably, they must be placed in a plugin rather than a theme.
Demo example A: Switch to theme if viewing a category archive page.
/**
* Switch to theme if front page.
**/
TSP_Manager::register(
'twentytwentyone',
function () {
return tsp_is_front_page();
},
1
);
Demo example B: Switch to theme if viewing a category archive page.
/**
* Switch to theme if viewing a category archive page.
**/
TSP_Manager::register(
'twentytwentytwo',
function () {
return tsp_is_tax( 'category' );
},
1
);
Demo example C: Switch to a custom theme if viewing a post with post ID.
/**
* Switch to a custom theme if viewing a post with post ID.
**/
$post_id_from_settings = 1; // Change value to your desired post ID.
TSP_Manager::register(
'twentytwentythree',
function () use ( $post_id_from_settings ) {
return ( $post_id_from_settings === tsp_get_post_id() ) ? true : false;
},
1
);
Demo example D: Switch to a custom theme based on post date.
/**
* Switch to a custom theme if viewing a post with post ID.
**/
$post_id_from_settings = 1; // Change value to your desired post ID.
TSP_Manager::register(
'twentytwentythree',
function () use ( $post_id_from_settings ) {
return ( get_the_date( 'Y-m-d', $post_id ) > date('Y-m-d') ) ? true : false;
},
1
);