1

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.

4

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() ); ?>
  • Just to comment on your use of syntax, try to avoid : and endif. Yes, it is proper valid php, but for the sake of debugging and readability, this syntax is not the most user friendly. The problem is, most code editors don't support this type of syntax, so it makes debugging a nightmare. I still prefer the old curlies ({}) as most code editors supports it, making debugging a breeze. But again, you should use what suits your best :-) – Pieter Goosen Nov 7 '14 at 8:16
  • 1
    Pieter, good point. Edited my answer appropriately. Thanks for pointing this out. – dMcClintock Nov 7 '14 at 8:19
  • +1, and not just for the update, but for the overall completeness and proper explanation of what your code and concept is all about. – Pieter Goosen Nov 7 '14 at 8:58
  • @dMcClintock thank you for the detailed answer! ` is_singular()` seems to work! :) – at least three characters Nov 7 '14 at 20:16
  • Glad to hear it's working. @PieterGoosen thanks for the vote and your feedback. – dMcClintock Nov 7 '14 at 21:59
2

I found a good way to check using is_singular( 'portfolio' )

1

To get the post type I would use something like this:

global $post;

get_post_type( $post );

This will return the post type of any post.

  • No, not every post is a global object. – fuxia Nov 6 '14 at 18:47
  • This is from WordPress "While inside the loop, these globals are set, containing information about the current post being processed." codex.wordpress.org/Global_Variables – Kyle Nov 6 '14 at 18:55
  • So what? The global variable can be overwritten from everyone before your check runs. That's the point of global variables: global access. – fuxia Nov 6 '14 at 19:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.