• Home
  • About Mitch
  • Speaking
  • Articles
  • Contact
  • Home
  • About Mitch
  • Speaking
  • Articles
  • Contact

Digital Strategist

WordPress Developer

Content Creator

Unapologetic Punk

Mitch Canter

  • X
  • Bluesky
  • GitHub
  • Twitch
  • YouTube
  • LinkedIn
Tutorial

Checking for A Specific Post Type in WordPress

CMDR Mitchcraft

Reading time: 2 minutes

Conditional Tags are one of my all-time favorite tools to use when developing a WordPress theme.  It’s an intelligent use of programming logic that allows even non-coders to grasp the basics and, in turn, create complex and functional WordPress themes.  With the advent of custom post types, a method was needed to check for the presence of a certain post type, and display the data appropriately.  There’s not a true conditional tag, in the sense you fill in a parameter of a function and it works.  Instead, you have to use a separate function value and test for a match.  In English, here’s how to check for a specific post type:

[code]

<?php if ( ‘book’ == get_post_type() ) {

//code goes here for book-type content

} ?>

[/code]

If you need to display an either/or content string, the code doesn’t change much:

[code]

<?php if ( ‘book’ == get_post_type() ) {

//code goes here for book-type content

} else {

//code goes here for all non book-type content

} ?>

[/code]

A good example of this would be my portfolio page.  If both the portfolio post type and my normal blog posts were sharing a template, but I needed to add a Pinterest Button to my portfolio, but not to my blog posts, it may look something like this:

[code]

<?php if ( ‘portfolio’ == get_post_type() ) { ?>

<a href=”http://pinterest.com/pin/create/button/?url=<?php echo urlencode(get_permalink($post->ID)); ?>&media=<?php echo $pinterestimage[0]; ?>&description=<?php the_title(); ?>” count-layout=”horizontal”>Pin It</a>

<?php } ?>

[/code]

The only problem is there’s no way to use the bang symbol in front (like a typical post type) to show that it is NOT the value specified.  Instead, we have to change the operator from “Equals” to “Does Not Equal”, like so:

[code]

<?php if ( ‘book’ != get_post_type() ) {

//code goes here for non book-type content

} ?>

[/code]

So, more than a few snippits to get you showcasing your post-type specific data quickly and easily.

conditional tag, custom post type, get_post_type, WordPress
  • Getting a Favicon on your WordPress Based Site

    Getting a Favicon on your WordPress Based Site

    Reading time: 2 minutes

    A good favicon (the little icon next to your URL in the address bar) can mean the difference between a boring bookmark, or standing out in a bookmark list.  It’s also something most people don’t think about, simply because it’s such a small part of the overall presence of a site.  Here’s the thing, though: having…

    WordPress
  • Go Update W3 Total Cache, WPTouch, and AddThis RIGHT NOW!

    Go Update W3 Total Cache, WPTouch, and AddThis RIGHT NOW!

    Reading time: 1 minute

    If you’ve updated your plugins in the last 24 hours, go straight back into your website and do it again – there’s a chance you may have downloaded some infected plugins that were hacked into the repository. According to WordPress.org, the plugins AddThis, W3 Total Cache, and WPTouch were infected with a backdoor that lets…

    WordPress