2

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.

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

you can use this for

global $post;

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

      <!-- do your stuff here -->

<?php } ?>

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

  • 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 --> } ?> – Telarian Nov 16 '18 at 18:43
  • Yes you can call your function in <!-- do your stuff here --> – Waqas Shakeel Nov 17 '18 at 5:50
  • Hmm. Not working for me. I suppose I'll make a post. – Telarian Nov 19 '18 at 15:07
4

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;
?>
  • 1
    Using the slug is the best solution – Rob Aug 10 '18 at 4:43
1

try to use is_single($post)

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

more here

1

Hooks such as init will not work at all.

You have to hook at least on parse_query.

This is the part of the code for the is_page function in WordPress core:

 * @param int|string|array $page Optional. Page ID, title, slug, path, or array of such. Default empty.
 * @return bool Whether the query is for an existing single page.
 */
public function is_page( $page = '' ) {
    if ( !$this->is_page )
        return false;

    if ( empty( $page ) )
        return true;

    $page_obj = $this->get_queried_object();

    $page = array_map( 'strval', (array) $page );

    if ( in_array( (string) $page_obj->ID, $page ) ) {
        return true;
    } elseif ( in_array( $page_obj->post_title, $page ) ) {
        return true;
    } elseif ( in_array( $page_obj->post_name, $page ) ) {
        return true;
    }

As you can see, everything bellow will work:

is_page(198); # (int) ID
is_page('198'); # (string) ID
is_page('Some Title'); (string) Title, case-sensitive
is_page('some-title'); (string) 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

0

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

is_page(34)
  • 2
    This answer needs some more explanation – cjbj Aug 29 '16 at 14:42

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.