null
null

Reputation: 416

WordPress ignoring is_page() conditional

<?php if( is_home()  || is_page(10)): ?> 
    <div class="text-center col-xl-12 col-lg-12 col-md-12 col-sm-12 col-xs-12">
<?php elseif(is_single() && !is_page(10)): ?>
    <div class="text-center col-xl-9 col-lg-9 col-md-9 col-xs-12">
<?php endif; ?>

All I'm trying to do is change the classes assigned to this <div> if you are on a single post, which works fine, but I can't get it under control when viewing a contact page, which it seems to recognize as a single post. This is fine, but I want to exclude one page from this effect.

I've tried is_page('Contact'), is_page('contact'), and is_page(10) None of them seem to return true in any case, I even deleted all other conditionals and just had it print something if its the contact page, but it still doesn't return true and has no effect.

I've done a lot of searching and all I can find says I'm using it correctly.. I'm at a loss as to why it wouldn't return true?

Am I using php if statements incorrectly and it's been an 'oops, it worked' scenario thus far? The url to the page ends with ?page_id=10, the page is titled 'Contact'

I could create a page template, but it seems more than what I need here, the contact page is painfully simple, and all I really need to do is center the four lines of text appropriately using bootstrap. Works fine across all other pages of the website, and changes the <div> appropriately if you're on the homepage or viewing a single post.

As requested, the full index.php code is below. Thanks to anyone who is willing to look at this. It is nothing massive, I am relatively new so please excuse my excessive commenting - I'm still learning. Full index.php: https://pastebin.com/nnG0ny99

Upvotes: 0

Views: 686

Answers (3)

Joscplan
Joscplan

Reputation: 1034

You can try using:

global $post;

<?php if( is_home()  || $post->ID == 10): ?> 
<div class="text-center col-xl-12 col-lg-12 col-md-12 col-sm-12 col-xs-12">
<?php elseif(is_singular() && $post->ID != 10): ?>
<div class="text-center col-xl-9 col-lg-9 col-md-9 col-xs-12">
<?php endif; ?>

That way you are getting the actual post id rather than the page id.

Edit: try to change is_single() to is_singular() as saNs suggested in his answer.

Upvotes: 2

saNs
saNs

Reputation: 157

is_single() works for "any post type, except attachments and pages"(https://developer.wordpress.org/reference/functions/is_single/) So it will always evaluate to false for pages.

Use is_singular() instead.

Upvotes: 1

dan webb
dan webb

Reputation: 646

Make sure you're not using is_page() in the loop.

From https://developer.wordpress.org/reference/functions/is_page/ "Due to certain global variables being overwritten during The Loop, is_page() will not work. In order to call it after The Loop, you must call wp_reset_query() first."

Upvotes: 1

Related Questions