0

I am developing a theme and I noticed that

get_post_meta($post->ID, '_wp_page_template', true);

returns 'static-page.php' (as expected) when queried on static pages but returns nothing (expected 'single-post.php') when queried on a single blog post pages.

I have single-post.php file set up and working, content displayed, etc. But I can't get template file name for some reason on this page. It works just fine on static pages. Any ideas why?

2
  • 1
    _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. May 29, 2019 at 5:53
  • I am using the template file name in the html <head> section to load corresponding css files. For static-page.php I am loading static-page.css, similarly for single-post.php I would like to load single-post.css. As said, it is working fine on my static pages, but returns zero on single blog post page.
    – sergekol
    May 29, 2019 at 5:56

2 Answers 2

1

I am using the template file name in the section to load corresponding css files. For static-page.php I am loading static-page.css, similarly for single-post.php I would like to load single-post.css. As said, it is working fine on my static pages, but returns zero on single blog post page.

This isn't really the correct way to do this. For starters you shouldn't be placing anything in <head> directly. Instead you should be 'enqueueing' the styles from your functions file, as outlined in the Theme Developer Handbook.

When you do it this way you can use the Conditional Tags to determine what is currently being viewed, and enqueue the appropriate styles.

For example, if you want to load styles for single posts only, you'd use is_singular( 'post' )., while for custom page templates you'd use is_page_template( 'static-page.php' );.

1
  • Thank you Jacob for explanation and references. This method of using is_single () works just fine. I am aware of WP recommendations and will definitely move to WP suggested method at a later stage. I haven't learned that yet, and I need to get quick result at this stage.
    – sergekol
    May 29, 2019 at 7:09
0

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.