Hi Guys, I’m trying to show a message on specific posts but the code below returns nothing when viewing any of the posts within the array.
It does however show the Post ID if I echo this using get_the_ID(); below.
if ( is_single(array(16743,29528,27647,25667,25251,19251,21043,17323,13418,28441,25122,22657,20963,17428)) ) {
echo '<div id="mk-alert" style="border:2px solid #D90368; color: #D90368; font-weight:bold; padding:20px; margin-bottom:20px;">TEST MESSAGE</div>';
}
$mk_post_id = get_the_ID();
echo $mk_post_id;
Why does if(is_single(array()) not work and get_the_id() does?
Thanks
Matt
Hello Matt!
For customization and other services, you can contact our custom work team here: https://tagdiv.com/premium-customization-services/.
Thank you for understanding!
Hi Bettina, thanks for the reply, I don’t need customisation services, I simply need to know why is_single() doesn’t seem to work, even when just checking a single post and not an array.
Thanks
Matt
Hello Matt!
Because the cloud template is actually showing when you visit a single post page. The cloud template acts like a proxy between the visitor and the post.
get_the_id() works because cloud templates are CPTs.
I hope it is more understandable now!
Thank you!
Hi Bettina, I get that cloud templates are CPTs but when echoed on the page, get_the_id() shows the ID of the currently queried single post, not of the CPT template file.
When we use a conditional statement using if ( is_single (12345) ); it doesn’t recognise the 12345. We’ll have to try and design a workaround if you’re suggesting this is custom work. We’d still have expected core WP functions to work within templates.
Thanks
Matt
Hi, given that is_single() doesn’t seem to identify the current post ID I’ve managed to get around this by using get_the_ID() which does seem to be able to identify the current post ID.
$current_post_id = get_the_ID();
$post_id_array = array(1,2,3,4,5);
if (in_array($current_post_id, $post_id_array))
{
echo 'CUSTOM ALERT';
}
This seems to work fine.
Thanks
Matt
