18 Responses

  1. Marlon
    Marlon Published |

    Excelent as usual, thanks.

    Reply
  2. Mike Schinkel
    Mike Schinkel Published |

    This is great, nice article!

    OTOH you know where the get_post_type() doesn’t work reliably, and where it’s needed for certain types of plugin development is within the admin. I have a particularly gnarly set of functions I’ve written that does it’s best to tease out the $post_type by first teasing out the $post by first teasing out the $post_id. To get the $post_id requires looking into $_GET for $_GET['post'] or $_GET[$_GET['post_type']], or $_POST['post_ID'] or $_POST['post_id'], or in $wp_query->query_vars for ‘p’ $wp_query->query_vars[$wp_query->query_vars['post_type'], or get_page_by_path($qv['pagename'],OBJECT,’page’)->ID or get_page_by_path($_SERVER['REQUEST_URI']), or a bit of other nastiness. (Wish that WP core would simplify and then maintain this type of code for us.)

    But I digress. :)

    Reply
  3. Ray Gulick
    Ray Gulick Published |

    Thanks, Justin, for straightening this out. I saw that other article and thought it looked odd, and wondered what it was supposed to accomplish that couldn’t be accomplished by the code you’ve shared above; another reminder that you can’t believe everything you read on the internet.

    Reply
  4. goto10
    goto10 Published |

    Thanks Justin! DevPress is in my rotation now that you’re on board. Also, thanks to Mike. Definitely been there before, but wasn’t aware of all of the teasers you listed.

    Reply
  5. Weekly WordPress Review » WPCanada
  6. Chuck
    Chuck Published |

    How do you check if is_singular() is a custom post type or not if you don’t know the cpt name/slug? So if you’re checking, lets say in wp_head, if is_singular(‘post’) how do you also see if it’s a CPT?

    Reply
  7. chuckreynolds
    chuckreynolds Published |

    How do you check if is_singular() is a custom post type or not if you don’t know the cpt name/slug? So if you’re checking, lets say in wp_head, if is_singular(‘post’) how do you also see if it’s a CPT?

    Reply
  8. WordPress Custom Post Types Tutorials, Tools & Advice | Customize WordPress Blog
  9. John Mitchell
    John Mitchell Published |

    I am using the news template for the site. Have made a few mods to fit the needs. I do need to have a few pages that do not display any sidebar and use this added area as part of the page content. Have not been able to figure this out. What is an easy way to do this in news template? (Great template, by the way!) Thanks!

    Reply
  10. stylomat
    stylomat Published |

    Oh my gooood!! Thank you so much – this was so important for my new wp project. thanks thanks thanks! :-)

    Reply
  11. Rabin
    Rabin Published |

    Thanks Justin, for easy and clear explanation as usual.

    Reply
  12. Robbi
    Robbi Published |

    Hi Justin, thanks for this! What would the solution be for an individual post within a custom post type? I’ve tried everything! :(

    Reply
  13. Rick
    Rick Published |

    Thanks Justin! Needed to check if custom_post_type, so easy :)

    Cheers,

    Rick

    Reply
  14. Gerson
    Gerson Published |

    Thank you. Perfect

    Reply
  15. Martin
    Martin Published |

    Thank you! I’m trying to check a post type category but I’m stuck;

    I have:

    if (‘portfolio’ == get_post_type() ){

    //Run some code

    }

    I want to do something like (for posts of a given type in a given category):

    if (‘portfolio’ == get_post_type(is_in category “X”) ) {

    //Run Some code

    }

    Do you know how I can do that?

    Thanks in advance.

    Reply
    1. Gerald
      Gerald Published |

      Martin,

      untested but adding a second conditional tag should do the trick:

      if (‘portfolio’ == get_post_type() && is_category( array( 'term-slug', 'term-slug2' ) ) { ... }

      http://codex.wordpress.org/Function_Reference/is_category

      Reply
  16. 4 Wordpress conditional tags you probably don’t know | SocialsPost.com

Leave a Reply

By submitting a comment here you grant this site a perpetual license to reproduce your words and name/Web site in attribution.

Please use your real name or a pseudonym (i.e., pen name, alias, nom de plume) when commenting. If you add your site name, company name, or something completely random, I'll likely change it to whatever I want.

css.php