6

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.

3
  • 1
    You did double-check that your are on the page with ID 346, right?
    – kraftner
    Mar 19, 2014 at 11:11
  • 3
    If this is another contenttype, try using if ( get_the_ID() == 346 ).
    – fischi
    Mar 19, 2014 at 11:13
  • Yes kraftner. I changed tact and started using WP Content Experiments & Event Tracking, which works for me.
    – Steve
    Mar 19, 2014 at 11:15

8 Answers 8

13

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.

5
  • What if I want to add a PHP function in <!-- do stuff -->? Do I just use <?php if( $post->ID == 346) { <!-- do your stuff here --> } ?> Nov 16, 2018 at 18:43
  • Yes you can call your function in <!-- do your stuff here --> Nov 17, 2018 at 5:50
  • Hmm. Not working for me. I suppose I'll make a post. Nov 19, 2018 at 15:07
  • Is global $post always available? Will it always be an object with an ID? What about on archive pages? Jan 4 at 23:02
  • @Firsh-justifiedgrid.com yes the variable is always available doesn't matter it is page, post or attachment or any custom post type. Jan 6 at 7:47
7

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;
?>
4
  • 1
    Using the slug is the best solution
    – Rob
    Aug 10, 2018 at 4:43
  • 1
    If the admin decides to change the slug of the post in the future, would that break this condition? Apr 15, 2020 at 8:16
  • 1
    @ViktorBorítás Yes it will. If you usually use the WordPress Import/Export features during development you're not guaranteed to have the same page ID on all your servers. If you deploy the whole database each time, then you'll get the same page ID. Else you can use Page title or slug.
    – RRikesh
    Apr 21, 2020 at 4:45
  • 2
    @RRikesh right, however in my opinion referring to page ID is still the safest strategy on the long run (especially if WP's fancy native internal redirection got overwritten by Devs), to break as few things as possible at a possible slug/Title/name change. That can happen just too easily. ;) I guess/hope in most cases Devs usually mirror the whole DB, so page ID-s stay the same. Apr 28, 2020 at 10:36
5

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

3

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; ?>
2

try to use is_single($post)

in your case is_single(346) or is_single('346') both should work

more here

2

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.

0

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

is_page(34)
1
  • 2
    This answer needs some more explanation
    – cjbj
    Aug 29, 2016 at 14:42
-1
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

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.