if (is_page(**PAGE ID**)) not working

I’m following this tutorial on adding Google Content Experiments code to header.php.

I added the following code to header.php:

<?php if (is_page('346') ):?>
    <!-- Google Analytics Content Experiment code -->
        ...
    <!-- End of Google Analytics Content Experiment code -->
<?php endif; ?>

This didn’t produce the content experiment code on the front end. I tried:

<?php if (is_page(346) ):?>
    <!-- Google Analytics Content Experiment code -->
        ...
    <!-- End of Google Analytics Content Experiment code -->
<?php endif; ?>

This didn’t work either.

Can you see why this code is not working? Thanks.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

you can use this for

<?php 
global $post;

if( $post->ID == 346) { ?>

      <!-- do your stuff here -->

<?php } ?>

you can use this anywhere either in header or anywhere else.

Method 2

A simpler solution will be to pass the title or the slug as argument in is_page(). You won’t have issues if you duplicate that page on another server.

<?php
if (is_page( 'Page Title' ) ):
  # Do your stuff
endif;
?>

Method 3

Hooks such as init will not work at all.

You have to hook at least on parse_query.

Everything bellow will work:

is_page(198); # ID (int)
is_page('198'); # ID (string)
is_page('Some Title'); # Title, case-sensitive
is_page('some-title'); # Slug

But it must be hooked at least in parse_query or any other hook after it. You can see WordPress hook order here: https://codex.wordpress.org/Plugin_API/Action_Reference

Method 4

First you have to know the difference between a page and post. Once you have done that then you can choose whether to use is_page or is_single.

If you are dealing with WordPress pages, then write in this way below. Note, this example is using array just in case if you want to implement it in many pages:

<?php if (is_page( array( 1, 529, 'or post title'  ) ) ) : ?>
    <!-- Do nothing -->
<?php else : ?>
    <!-- Insert your code here -->
<?php endif; ?>

But if you need it to take effect also on your posts, then add this lines too:

<?php if (is_single( array( 1, 529, 'or post title'  ) ) ) : ?>
    <!-- Do nothing -->
<?php else : ?>
    <!-- Insert your code here -->
<?php endif; ?>

Method 5

try to use is_single($post)

in your case is_single(346) or is_single(‘346’) both should work

more here

Method 6

For single posts use

if ( is_single( '1346' ) )

For single pages use

if ( is_page( '1346' ) )

Where '1346' is your post or page ID.

is_page will NOT work with single posts and is_single will not work with single pages.

Method 7

Please try to remove '' (single quotes) from ID number & it will work:

is_page(34)

Method 8

function test_run(){

   if (is_page( 'Page Title' ) ): //you can use is_page(int post id/slug/title)
      # Do your stuff
   endif;

}

add_action('parse_query', 'test_run');

completing @Lucas Bustamante ‘s answer


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x