WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Targeting just the single pages of the blog not custom post types (9 posts)

  1. devoninternational
    Member
    Posted 10 months ago #

    Hello I have certain content I would like to exclude from the blog is_single() pages, but when I use ! is_single() it removes the content from my custom post types as well since they are is_single() pages.

    Is there a way to target just the ! is_single() blog pages and leave the is_single() pages of custom post types alone?

    Thanks!

  2. blogjunkie
    Member
    Posted 10 months ago #

    Hi, you can use the get_post_type() function. e.g.

    if ( get_post_type() == 'book' ) { ... }

    Here's a handy page to bookmark: http://codex.wordpress.org/Conditional_Tags

    :)

  3. devoninternational
    Member
    Posted 10 months ago #

    thanks, in more detail and in words I am trying to say if it is NOT a single page excluding the custom post types

    so my weak attempt was:

    if ( ! is_single() but somehow exclude CPT single pages )

    I also tried the blog page ID in the post parent conditional but it did not work:

    if ( ! $post->post_parent == '36' )

  4. blogjunkie
    Member
    Posted 10 months ago #

    Does this mean you only want to target Posts, but not CPTs? You can also use get_post_type() for regular Posts.

    if ( get_post_type() == 'post' ) {
    // this code only runs for Posts
    }

    Or combine them

    if ( get_post_type() == 'post' || get_post_type() == 'book' ) {
    // this code only runs is the current post is a regular Post or Book CPT
    }

    Hint: regular 'ol Posts are a CPT too ("Posts" CPT)

  5. devoninternational
    Member
    Posted 10 months ago #

    This seems to target even the posts of the CPT, just wondering how I can separate btw/ blog single post pages and CPT single post pages.

    if ( get_post_type() == 'post' ) {
    // this code only runs for Posts
    }
  6. devoninternational
    Member
    Posted 10 months ago #

    OK not sure if this is a workaround, but I excluded the single pages but then added in the CPT single pages. So basically a banner image shows for all the CPT single pages but not the blog post single pages.

    if ( ! is_single() || get_post_type() == 'landing_pages'  ) {
    // Show banner images for CPT single pages but not blog post single pages
    }

    Duh, can't believe I stumbled there, if anyone has cleaner code please let me know and thanks for the help blogjunkie

  7. blogjunkie
    Member
    Posted 10 months ago #

    Doesn't it work if you just do this?

    if ( get_post_type() == 'landing_pages'  ) {}

    That will only target landing_pages CPT single pages, not regular Post single pages

  8. devoninternational
    Member
    Posted 10 months ago #

    I had to exclude the single pages of the blog post otherwise the custom banners I was building were showing up on the blog single pages which I did not want.

  9. devoninternational
    Member
    Posted 9 months ago #

    Thanks for the help bogj

Reply

You must log in to post.

About this Topic