1

I am needing to show a different header for a specific page.

I created a custom page template that calls/shows the normal header that the entire website uses. I am using this page template for about 50 pages.

Now, I need only one of those pages to show a different header. This header will be very similar to the normal one, but I will use a different contact number (using this specific page for online ads).

To Create the New Header
I am thinking I could duplicate the code on the original header, change the mentioned contact number, name the header something like header-second.php.

Here is the only bit of code that will be changing for the new/second header. Only the phone number will change...

<div class="row-two">
<div style ="float: right; margin-right: 16px; margin-top: 10px;"><p 
class="headersample1"><a href="/request-a-quote/">REQUEST QUOTE</a></p></div>
<div style ="float: right; margin-top: 10px;"><p class="headersample2">OR 
</p></div>
<div style ="float: right; margin-top: 10px;"><p class="headersample3"><a 
href="tel:555-555-5555">555-555-5555</a></p></div>
</div>

Assign New Header to Specific Page
Any ideas how I can assign a different header to show on a specific post ID? This page uses the same custom page template that about 50 other pages use, so I can't call this new header in the custom page template...I don't think.

3
  • How much of your header changes? If it is only the phone number, a very easy solution is a condition in the header.php file.
    – jdm2112
    Feb 28, 2020 at 20:16
  • Edited original question with the code used in the header.php file.
    – Webman
    Feb 28, 2020 at 20:40
  • If only the phone number changes, I would definitely recommend the first example. I will edit to be more explicit to your code.
    – jdm2112
    Feb 28, 2020 at 20:41

1 Answer 1

2

In your header.php file, you can test which page is loading and modify the markup based on this condition. For your example, the is_page() function seems ideal.

<div style ="float: right; margin-top: 10px;">
    <p class="headersample3">
    <?php if ( is_page( '###' ) ) { ?>
        <a href="tel:555-555-5555">555-555-5555</a>
    <?php } else { ?>
        <a href="tel:888-888-8888">888-888-8888</a>
    <?php } ?>
    </p>
</div>

Replacing the ### with your actual page ID.

If you truly need to load an entirely different header file for this one page, then you would test is_page() in the page template and then conditionally load the header file you want.

if ( is_page( '543' ) {
    get_header( '543' );  // file name is actually header-543.php
} else {
    get_header();
}

Note: The parameter passed to get_header() is not the full file name. See the Codex for more detail https://developer.wordpress.org/reference/functions/get_header/

Also, a good naming practice is to follow the WP core standards. A header for a specific page ID should be named header-pageid.php

EDIT: updated to reflect code added to question.

5
  • 1
    This updated code is working exactly as I needed! <div style ="float: right; margin-top: 10px;"> <p class="headersample3"> <?php if ( is_page( '###' ) ) { ?> <a href="tel:555-555-5555">555-555-5555</a> <?php } else { ?> <a href="tel:888-888-8888">888-888-8888</a> <?php } ?> </p> </div>
    – Webman
    Feb 28, 2020 at 21:20
  • This is great to target one specific page. Is it possible to target more than one page? I tried these examples, but nothing worked: <?php if ( is_page( '123456,123457' ) ) { ?> <?php if ( is_page( 123456,123457 ) ) { ?>
    – Webman
    Feb 28, 2020 at 21:21
  • is_page() accepts an array. Your examples are not valid PHP arrays. Try is_page( array( '123', '456', '789' ) ). You might also create the array separately and then use that variable as the argument e.g. is_page( $page_ids )
    – jdm2112
    Feb 28, 2020 at 21:30
  • Works perfectly! Thank you for your help!!
    – Webman
    Feb 28, 2020 at 22:21
  • Glad it worked for you. Happy to help.
    – jdm2112
    Mar 1, 2020 at 15:07

Your Answer

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

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