0

I had this old piece of code from CSS Tricks that was for making it so I could conditionally target child pages of a parent. It worked great until I changed my content to a custom post type. Now the code no longer works, as it was specifically for pages.

    function is_tree($pid) { // $pid = The ID of the page we're looking for pages underneath
        global $post; // load details about this page
        if(is_page()&&($post->post_parent==$pid||is_page($pid))) 
          return true; // we're at the page or at a sub page
        else 
          return false; // we're elsewhere
    };

I adapted it, now using is_singular('post_type'). The problem is that it always seems to meet the first condition and never hits the second elseif. I can't seem to figure out why exactly it isn't working.

    function is_tree($pid) {  
        global $post; 
        if(is_singular('guides' )&&($post->post_parent==$pid||is_singular( 'guides', $pid ))) 
          return true;   
        else 
          return false;  
    };

Any idea on what might be going wrong here?

1 Answer 1

1

is_singular only accepts one argument- post type. It's only for checking if a post is of a certain type(s), not a specific post of that type. Check $post->ID instead-

if(is_singular('guides' )&&($post->post_parent==$pid||$post->ID==$pid))

I would also consider using get_queried_object() and/or get_queried_object_id() rather than use the global $post.

1
  • Perfect, that seems to have worked. Now for the get_queried_object, how do use that in this context? I tried just replacing global get_queried_object_id() and got an error and none of the documentation I've read seems to match this context(sorry not a developer here).
    – jbwharris
    Feb 5, 2018 at 13:35

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.