2

I'm trying to serve different headers based on what type of page the user is on. This is my code.

<!-- IF HOME -->
        <?php if ( is_front_page() && is_home() ) : ?>
        <?php get_template_part( 'template-parts/headers/home-header' ); ?> 

        <!-- IF TEMPLATES -->

        <?php elseif ( is_page_template('archive-mobile_photo.php') ) : ?>
        <?php get_template_part( 'template-parts/headers/home-header' ); ?> 

        <!-- IF  POST -->
        <?php else : ?>
        <?php get_template_part( 'template-parts/headers/zine-header' ); ?>     

        <?php endif; ?>

What's weird is that the homepage and post pages are working fine, but the check using is_page_template() isn't working. I have the query monitor plugin and it's confirming the page is the archive-mobile_photo.php template.

I'm pretty new to WordPress and I'm at a total loss.

  • Where is the template file located in the theme directory ? – bravokeyl Sep 20 '16 at 4:28
  • It's in the same directory as the the file I'm working in. I kept checking to make sure though. – Garrett Scafani Sep 20 '16 at 4:39
1

It looks like you're checking if you're on the mobile_photo post type archive with this line:

<?php elseif ( is_page_template( 'archive-mobile_photo.php' ) ) : ?>

If that's indeed the case, use is_post_type_archive( $post_types ) instead:

<?php elseif ( is_post_type_archive( 'mobile_photo' ) ) : ?>
  • Bless you kind sir! How would I have come to this? Just read the documentation until I've tried everything? I should probably do that. Thanks!! – Garrett Scafani Sep 20 '16 at 4:40
  • You got it! I didn't see the problem at first glance, but the details in your question (thank you for that) pointed me to is_page_template(). I recognized the pattern being used as archive-{post_type}.php from the ever-important template hierarchy, and thought we may have had a winner. – Dave Romsey Sep 20 '16 at 4:46
0

Rather than using the conditionals to load headers as template parts, it would be more readable to use the functionality built in to get_header() when you call it for each page template. These files would live in the root of the theme folder.

You would name your two files header-home.php and header-zine.php and call them in the page template with:

get_header('home');

and

get_header('zine');

If you do need the conditionals for a more complex page, you can still check the page and load the appropriate file based on your criteria. There's an example of that in the codex as well.

I'm not sure what the difference is between get_header() and get_template_part() but if nothing else, I think it's more readable, and follows the WordPress standard.

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.