0

I am attempting to ensure a widget is only loaded on blog post pages.

I tried using $post_type = get_post_type( $post_id ); inside the widget function of my widget class. However, upon loading pages like the homepage, 'archives', 'categories' etc. . . the post type is always 'post'. The only exception is when I create a page, it returns 'page' as expected.

Is there a way to actually ensure the current page is a post?

1 Answer 1

1

I think you're looking for is_single():

if ( is_single() ) {
    // Do widget, which will only appear on single-post templates
}
2
  • This will still allow it to load on 'page' post types. However, this solved the problem I was having. I can implement another filter with get_post_type() in addition to using is_single() if I really need to limit to just posts. Thank you! Commented Oct 15, 2015 at 23:28
  • 1
    Makes sense, you could do something like if (is_single() && !is_page()) { do_widget(); } Commented Oct 15, 2015 at 23:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.