2

I'm using the following if statements within header.php to load custom tracking codes.

<?php if ( is_singular( array( 'custom-post-type', 'post-name' ) ) ) : ?>
     // Specific tracking code here
<?php endif ?>

<?php if ( is_singular( array( 'custom-post-type', 'another-post' ) ) ) : ?>
    // Specific tracking code here
<?php endif ?>

When I go to each post, both tracking codes are showing up. Am I doing something wrong here?

1
  • 3
    It's not clear if you want to target specific posts (with some slugs) or specific post types.
    – birgire
    Commented May 11, 2015 at 11:38

2 Answers 2

5

You are using an incorrect check here. is_singular() returns true when a post is from the specified post type or post types or the default post types when none is specified. You cannot target specific single posts with is_singular()

You have to use is_single to target a specific post

if ( is_single( 'post-a' ) {
    // Do something for post-a
} elseif ( is_single( 'post-b' ) {
    // Do something for post-b
}
3

I think you have over complicated your parameter for "is_singular", or is "custom-post-type" an actual CPT of yours?

Try:

if ( is_singular( 'custom-post-type' ) ) {
  // Foo
} else if ( is_singular( 'another-custom-post-type' ) ) {
  // Bar
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.