• Andre

    (@andre-jutras)


    Generally, when I want to add a class to the body tag based on when the page.php is the default page template and being used, I would use this function:

    if(basename(get_page_template()) === 'page.php'){
    $classes[] = 'default-page';
    }

    However, if singular.php is being used instead because page.php is not part of my theme, but I still want to add a body class if a page is using the default template, is there a method to achieve this?

    In a sense, the conditional would be that if the default page template is being used, add this class to the body when the default page is using singular.php.

Viewing 4 replies - 1 through 4 (of 4 total)
  • You know that WP adds classes for page template already, right?
    For single posts: post-template-default single single-post
    For pages: page-template-default page

    But your question is not quite about page template, since you are asking about singular.php. So it’s more about theme template file? Still, it seems that WP is already doing what you want.

    you can also check, possibly in connection with is_page():
    get_post_meta( $post->ID, '_wp_page_template', true )
    which would return ‘default’ in both cases whether page.php is used or does not exist in the theme.

    Thread Starter Andre

    (@andre-jutras)

    Hi Joy…. not quite because I want to add my own class that is specific to my theme, but to make it a shorter class too. I’m a bit finicky on classes that are too long, lol.

    Also, thanks Michael…I’m still looking into possibilities. In the meantime, I created a page.php template while keeping the singular.php (which has a conditional in it). So far, everything is working, but still curious if there are other alternatives too.

    If you look at https://core.trac.www.ads-software.com/browser/tags/5.3/src/wp-includes/template-loader.php#L49 where the decision is made for the template to use, you’ll see that $template is global. It is also filtered, so you could see if it’s changed from the default.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Conditional for Singular template if default page’ is closed to new replies.