• I’m having a mental block.

    I want to echo the post type name in my header.php file so that I can style it specific to the post type.

    Can anyone help?

    Thanks

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi,
    I’m not quite sure what you’re looking for. If you want to style the single post page for the custom post type, you can use the single-xxx (where xxx is the post type name) body class added by WP automatically.
    If you actually want to print the post type name, you can use the get_post_type() function: https://developer.wordpress.org/reference/functions/get_post_type/
    To add specific code in the header.php for the post type you can do something like this:

    if ( get_post_type() == 'your_custom_posttype' ) {
      // Do stuff
    }

    Hope that helps.

    Thread Starter candell

    (@candell)

    Thanks for the help, I had typed get_pot_type hence the issue. Sorry, the curse of lack of sleep strikes!

    global $wp_query;
    if ( $wp_query->get( 'post_type' ) === 'POSTTYPE' ) {
    
    }

    will work on the post type archive and single posts of that type.

    If you have a taxonomy archive in the mix you’ll need to check that separately:

    global $wp_query;
    if ( $wp_query->get( 'post_type' ) === 'POSTTYPE' || $wp_query->get( 'taxonomy' ) === 'TAXONOMY' ) {
    
    }

    Edit: Sorry, I misread that as you saying you’d tried get_post_type and it didn’t work. Oh well, there’s another solution.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Post Type Name in header’ is closed to new replies.