problems with is_post_type() – perhaps it is a bug?
-
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.
-
I just tried your idea of using
is_singular($post_type)
and it didn’t work. Here is my sidebar.php sample:<?php // stats da anunciante if ( is_single() && is_singular('post') ) { include 'dashboard.php'; } ?> <?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar() ) : ?> [...]
Basicaly I am getting the same behaviour with
is_singular($post_type)
as if usingis_post_type()
, as described by our chap t310s_. That is, it returns true as long as $post_type is a registered post type in my WP system.Would you be kind enough to share your example with us, so we can see how you made it work? According to the is_singular article in the codex, it doesn’t have any example of this function supporting arguments (i.e.: post types, as in your example of
is_singular('post')
). If it does accept then the codex should be updated, and you would be doing the WP community a great favour and helping us with the documentation =)In the meantime, I will now try t31os_’s suggestion of using
$post_type = get_post_type( $post );
and see where I can go with it… =(I solved the problem with t31os_’s suggestion. Thanks a lot dude!
<?php $post_type = get_post_type( $post ); if ( is_single() && $post_type == 'post' ) { include 'dashboard.php'; } ?> <?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar() ) : ?>
Good stuff, happy to help.. 🙂
here the code i use.
<?php if(is_singular('product')) { ?> <?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar(Products) ) : ?> <?php endif; ?> <?php } else { ?> <?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar() ) : ?> <?php endif; ?> <?php } ?>
before this i use
is_single() && is_post_type($post_type)
but they doesnt seems to work.I confirmed that is_singular(‘post_type’) is working for fine! Indeed, the “is_post_type” function is poorly named…
I didn’t managed to make
is_singular('post_type')
to work. Butis_single() && $post_type == 'post'
worked.For is_singular to work with post_type you need to write it like this:
is_singular( $post_type = your_post_type )
This works for me.
Strangely, I couldn’t get this to work in my
functions.php
file.if(is_singular('missions')) { wp_enqueue_script('missionscript', get_bloginfo('template_url') . '/assets/js/mission.js', array('jquery'), '1', true); }
So I ended up having to just call it in my footer.
<?php if(is_singular('missions')) { // Only gets called on Mission pages ?> <script type="text/javascript" src="<?php bloginfo('template_url'); ?>/assets/js/mission.js"></script> <?php } ?>
Any idea why this wouldn’t work with the functions file?
I use
if ( 'post_type' == get_post_type())
Where post_type is the name of your custom post type. Works like a charm.
- The topic ‘problems with is_post_type() – perhaps it is a bug?’ is closed to new replies.