• Resolved pmdci

    (@pmdci)


    Hi there,

    I added the following conditional in my sidebar.php template, before the check for widgets:

    <?php if ( is_single() && is_post_type('hotel') ) { include 'stats-hotel.php'; } ?>

    The is_single() conditional works fine. So it only includes the stats-hotel.php file when viewing single posts.

    HOWEVER the is_post_Type() function doesn’t work properly. In my case, besides the default post type I also have hotel and resort as post types. Nonetheless the file stats-hotel.php is being included when viewing every post type I have (post, hotel, resort), when it should only be included when seeing the post type hotel.

    HOWEVER, if I change the is_post_type() argument and add some garble (i.e.: an unexistent post type), for example:

    is_post_type(‘garblegarblegarble’)

    Then the file stats-hotel.php is never included when seeing any type of post type.

    Am I doing something wrong, or this is really a bug?

    Regards,
    P.

Viewing 15 replies - 1 through 15 (of 24 total)
  • If you have a registered post type of “hotel” then this would always be true:

    is_post_type('hotel')

    You might want this instead:

    <?php
    $queried_post_type = get_query_var('post_type');
    if ( is_single() && 'hotel' ==  $queried_post_type ) {
      echo 'this single view is of a post type hotel';
    }
    ?>

    Note: thanks for fixing the Conditional Tags Codex article.

    Thread Starter pmdci

    (@pmdci)

    MichaelH,

    This is a great feedback thank you.
    (and thanks for noticing my fix in the codex 🙂 )

    I think I will update the codex with the code you have provided with a ‘Snipted ‘ with this code if I get it to work. Is that ok?

    But it didn’t work for me, though. Here is what I did:

    <?php // get stats dashboard
    $queried_post_type = get_query_var('post_type');
      if ( is_single() && 'post' ==  $queried_post_type ) { include 'dashboard.php'; } ?>

    I know I said in my previous example that the post type I was trying to query was hotel. But in all honesty I was just trying to simplify things. Perhaps I confused things even more?

    If so I apologise. But I need this include to happen ONLY in single pages of type post (the defaul post type).

    Even big WP Gurus on the internet, as per what I read in their blogs, thought that this function is_post_type() would work as I thought…

    Thread Starter pmdci

    (@pmdci)

    MichaelH,

    Please see my message above as I edited it. (I was counting victory before winning the battle…)

    I didn’t get the result I expected… 🙁

    As a matter of fact I tried to add a test of <?php echo $queried_post_type; ?> and I got nothing in return. Perhaps I must declare some variable global somewhere? What do you think?

    I also tried adding the following in my single.php file:

    <?php if (have_posts()) : while (have_posts()) : the_post();
      $queried_post_type = get_query_var('post_type');
      $city_as_text = get_the_term_list( $post->ID, 'city', '', ', ', '' );
      echo $queried_post_type; ?>

    But it also didn’t work…

    Evidently the query var for post_type doesn’t get populated on a post.

    Could be a bug.

    It does get set for the custom post types like your hotel.

    Maybe you might consider reporting that as a bug to trac.

    Thread Starter pmdci

    (@pmdci)

    MichaelH,

    I would be more than happy to repot bugs. I went to track before but I couldn’t see a link for creating an account…

    How can I do it?

    Regards,
    Pedro

    Use your forum login there.

    Thread Starter pmdci

    (@pmdci)

    Cool! It worked. I just raised the ticket here (#13535), making sure I pointed to this forum post.

    Thanks MichaelH. Hope you guys can make it work (that is, if it is a bug and not my fault, hehe)

    Regards,
    P.

    I’m not able to reproduce your issue, using echo get_query_var('post_type') prints out the relevant post type for a custom post type.

    Have a look back at your register_post_type code and check you have the query_var parameter set to true (if it’s set to false, then there’s the source of your problem).

    NOTE: I’ve noticed when registering post types manually, i’ve had to re-save(just click the save button) the permalink settings in order for the necessary rewrite rules to get created and added to the rewrite array (which you’ll need if using any permalinks, ie. non-default).

    I’m not able to reproduce your issue, using echo get_query_var(‘post_type’) prints out the relevant post type for a custom post type.

    But what if the post type is “post”?

    It’s empty when it’s a post or page, but i’d expect that because they’re builin post types which are already accounted for with their own set of rules.

    Yep, that is the question at hand 😉

    Ok, i see, so why not look at the $posts object?

    Off the top of my head..

    $post_type = get_post_type( $posts[0] );

    Or.

    $post_type = get_post_type( $post );

    Might need to global the necessary var in your sidebar, but it should be sufficient (i think).

    i got the same probs too. is_post_type(‘post’) doesnt works correctly.

    use is_singular($post_type) seems doing well.

    is_post_type is not a conditional statement like alot of the is_* functions, it’s intended to be used to check if a post_type exists.. (poorly named function).

    is_post_type( 'post' );

    ..would be true for any install of WordPress..

    is_post_type( 'book' );

    ..would be true for any install where a post type of book has been registered..

    With a bit of luck hopefully it will be replaced by something more appropriately named.
    http://core.trac.wordpress.org/ticket/13747

Viewing 15 replies - 1 through 15 (of 24 total)
  • The topic ‘problems with is_post_type() – perhaps it is a bug?’ is closed to new replies.