I am working today to upgrade my theme as per Jacob's earlier suggestion. I removed all the stylesheet links from header.php and put them into functions.php.
The non-conditional stylesheets and those stylesheets which are connected using is_page_template() work without any issue.
However, the stylesheets which are connected using is_category, is_tag, etc. do not work.
Here's the code:
function add_styles () {
// Styles for all pages - work just fine
wp_enqueue_style( 'normalize', get_template_directory_uri().'/css/normalize.css', false, '1', 'all' );
wp_enqueue_style( 'main', get_template_directory_uri().'/css/main.css', false, '1', 'all' );
wp_enqueue_style( 'header', get_template_directory_uri().'/css/header.css', false, '1', 'all' );
wp_enqueue_style( 'index', get_template_directory_uri().'/css/index.css', false, '1', 'all' );
wp_enqueue_style( 'functions', get_template_directory_uri().'/css/functions.css', false, '1', 'all' );
wp_enqueue_style( 'footer', get_template_directory_uri().'/css/footer.css', false, '1', 'all' );
// Styles for custom template pages - DO NOT work
if ( is_category() ) { wp_enqueue_style( 'category', get_template_directory_uri().'/css/category.css', false, '1', 'all' ); };
if ( is_tag() ) { wp_enqueue_style( 'tag', get_template_directory_uri().'/css/tag.css', false, '1', 'all' ); };
if ( is_single() ) { wp_enqueue_style( 'single', get_template_directory_uri().'/css/single-post.css', false, '1', 'all' ); };
if ( is_date() ) { wp_enqueue_style( 'date', get_template_directory_uri().'/css/date.css', false, '1', 'all' ); };
// Styles for static page templates - DO work
if ( is_page_template('front-page.php') ) { wp_enqueue_style( 'front-page', get_template_directory_uri().'/css/front-page.css', false, '1', 'all' ); };
if ( is_page_template('static-page.php') ) { wp_enqueue_style( 'static-page', get_template_directory_uri().'/css/static-page.css', false, '1', 'all' ); };
if ( is_page_template('landing-page.php') ) { wp_enqueue_style( 'landing-page', get_template_directory_uri().'/css/landing-page.css', false, '1', 'all' ); };
if ( is_page_template('404.php') ) { wp_enqueue_style( '404', get_template_directory_uri().'/css/404.css', false, '1', 'all' ); };
add_action( 'wp_enqueue_scripts', 'add_styles' );
It is clear that is_category, is_tag, etc. do not work. From reading CODEX and the internet I figured out that they are called from functions.php too early. However, I can't understand how to hook them properly so that they work.
What would be the right way to do this?
_wp_page_template
is the meta value that's stored when a post or page is using a custom template. It's not used if it's using the default template. Is there a particular reason you need to know the name? If it's just for debugging, then the Query Monitor plugin can tell you which templates are in use.