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?
2 Answers
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
}