admin管理员组

文章数量:1391947

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.

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.

Share Improve this question edited Feb 28, 2020 at 20:39 Webman asked Feb 28, 2020 at 20:14 WebmanWebman 631 silver badge11 bronze badges 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 Commented Feb 28, 2020 at 20:16
  • Edited original question with the code used in the header.php file. – Webman Commented 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 Commented Feb 28, 2020 at 20:41
Add a comment  | 

1 Answer 1

Reset to default 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/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.

本文标签: phpShow Different Header on a Specific Post ID