0

Trying to load conditional content thru the header, but only front page is being identified correctly. Archive, post and custom type templates aren't.

enter image description here

Using Query Monitor to identify correct template and used that template name as argument, with and without child theme folder declaration, but every other template falls into the else conditional.

    <?php
/* Conditional AD script loading */

/* Front page */
if ( is_home() || is_front_page() ) {
    echo "Front page";
} elseif ( is_page_template(  'single-listing.php' ) ) {
    echo "Listing page template";
} elseif ( is_archive() ) {
    echo "Archive page template";
} elseif ( is_page_template( $template  ='single.php' ) ) {
    echo "Post template";
} else {
    echo "wrong template";
}

?>

PHP elseif is in header of child theme, template files are within child theme folder too. What am I missing?

1

The is_page_template() function is for checking if the current page is using a specific custom page template. That being a template selected from the Template dropdown in the Page Attributes box when editing the page.

The WordPress conditional tags are not based around which template files are used. They are used to tell you what content is being viewed, regardless of which template files are present in a theme. For example, is_category() will return true when viewing a category archive, even if your theme does not have a category.php file.

The list of available conditional tags is documented here.

If you want to use Query Monitor to figure out what conditionals to use for what pages, use the Conditionals tab:

Screenshot of query monitor with the conditionals tab active

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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