I’m still a noob with php but I’m trying to display a different layout for my WordPress webpage for a single post id.
I thought it would have been simple but I’ve tried a quiet few variations of the code below. Including is_singular
and without the $post
etc. and I’ve ran out of inspiration. What can I do? What do I need to look for? Can anyone help me out?
<?php
if (is_single ($post="2578")) {
get_template_part('partials/content', 'challenge');
}
elseif (is_single ($post="")) {
get_template_part('partials/challenge/content', 'challenge-2');
get_template_part('partials/challenge/content', 'categories');
get_template_part('partials/challenge/content', 'snake-checklist');
get_template_part('partials/challenge/content', 'timeline');
}?>
3 Answers
This looks almost correct. Let’s have a look at is_single()
:
Works for any post type, except attachments and pages…
So if the given ID isn’t that of a page
or attachment
post type then you can use the function as so:
if( is_single( 2578 ) ) {
/* ... */
} else {
/* ... */
}
Otherwise, if the ID is a page
post type you can use is_page()
if( is_page( 2578 ) ) {
/* ... */
}
Should the ID be that of an attachment
page you may use is_attachment()
if( is_attachment( 2578 ) ) {
/* ... */
}
Finally, if you’re unsure of the post type you could check against the global $post
object, assuming it is correct:
global $post;
if( is_object( $post ) && 2578 == $post->ID ) {
/* ... */
}