Check for custom post types – WordPress

If you work with WordPress themes and Custom Post Types you will love the snippet below.

This is a conditional check for custom post types within The Loop:

<?php
if ( 'review' == get_post_type() ) {
	/* Custom code for 'review' post type. */
} elseif ( 'restaurant' == get_post_type() ) {
	/* Custom code for the 'restaurant' post type. */
}
?>

If you want to use the snippet outside the loop you just need to pass the post ID:

<?php
if ( 'product' == get_post_type( 123 ) ) {
	/* Do something */
}
?>

Learn more about the get_post_type function.

About Ricard Torres

Senior Front-end Software Engineer from Barcelona, Haidong Gumdo Instructor (korean martial art of the sword), street photographer, travel lover, TV addict, Boston Red Sox fan, and privacy advocate.

@ricard_dev @ricard_dev

📝 Blog 🎙 Podcast

Leave a Reply

Add <code> Some Code </code> by using this tags.

*
*