Check if post has a specific post type

I created a custom post type called “portfolio” for posts. When navigating to a post the URL shows as www.mysite/?portfolio=some-post-title

In footer.php I want to display something only if the post has the post type “portfolio”. How is this possible?

I also tried different options from http://codex.wordpress.org/Conditional_Tags to no avail.

3 Answers
3

First, check your syntax and ensure any comparison operators, escapes, and quotes are correct, and the statements are written properly based on the examples given in the codex. w3schools’ PHP Tutorial is a great starting point. Also, if your question provided the code you attempted to use with a conditional tag, it would be beneficial in allowing others to provide a specific answer.

Possible Solutions:

Adding the following to footer.php will display specific HTML content on single posts in the “portfolio” post type.

<?php if ( is_singular( 'portfolio' ) ) { ?>
    <!-- If this is a single "portfolio" post, do something cool here -->
<?php } ?>

A variation that includes your “portfolio” archives page as well as the single posts would be:

<?php if ( is_singular( 'portfolio' ) || is_post_type_archive( 'portfolio' ) ) { ?>

If the code above does not work:

Verify that your conditional statement is checking for the correct post type name, not a taxonomy or term (easy to confuse if similarly named). Also keep in mind that depending on how the CPT and it’s objects are setup (specifically url rewrite parameters), the url slug will not necessarily match the post type name.

You can verify the name of the post type by echoing the return value of the “get_post_type” function:

<?php echo 'The post type is: ' . get_post_type( get_the_ID() ); ?>

Leave a Comment