WordPress.org

Ready to get started?Download WordPress

Forums

How to check for custom post type and if it's hierarchical (6 posts)

  1. Chuck Reynolds
    Member
    Posted 2 years ago #

    I have this right now in a plugin running in wp_head.

    if (is_singular('post')) {
    			echo "\t<meta property='og:type' content='article' />\n";
    		}else{
    			echo "\t<meta property='og:type' content='website' />\n";
    		}

    It works good so that pages always show as og:type website and posts show as og:type article - as it should be.

    Problem is if the site using the plugin has a CPT (custom post type) this og:type will always display website, when if you're using a CPT as a post then it should display that as an article og:type.

    I don't know how to check if the url loading is a CPT and how to change that og:type accordingly. Thoughts were to check if it's hierarchical? If the cpt is set to that in theory that would be like a page therefore the og:type should be 'website', and visa-versa if the cpt is not hierarchical then og:type in theory should be 'article'.

    Tried this but isn't working completely right.. it's making the homepage 'article' when it should be 'website' because it's a page not a post.
    if (is_singular('post') || is_post_type_hierarchical(get_post_type())) {

    Tried to write this as clearly as I could... hope that make sense.

  2. Andrew Norcross
    Member
    Posted 2 years ago #

    The issue I believe is that people use CPTs differently (regardless of whether or not it's hierarchical or not). Example: people create a "services" CPT that is hierarchical, but each individual item could have information that would make the end user want to use og:type article instead of website.

  3. What identifies something as an 'article' versus a 'website?' I think once there is definitive criteria, we can more easily shape the logic to match.

  4. Chuck Reynolds
    Member
    Posted 2 years ago #

    @norcross yeah. I explained this to you on twitter but just for everybody else in this thread...
    Basically the plugin has one required item and one option - that's it. I like that... less options = more seamless and less obtrusive for users and less explanation. Just install it and it works kind of mentality. IF I could do this all programmatically I'd much rather it be that way.
    As Andrew and I talked on twitter it may have to come down to me displaying any/all CPT's in the plugin admin and next to each have a dropdown list allowing the user to select the og:type individually. Not really what I want but I suppose if they're doing CPT's they should have an idea of what each CPT should relay to Facebook/Google+1

    @J3 to me an article is just that lol... a website is a static page or a root/index type thing.

  5. Helen Hou-Sandi
    WordPress Dev
    Posted 2 years ago #

    Did you try what I suggested earlier?

    if ( is_singular() && !is_post_type_hierarchical( get_post_type() ) ) {

    I think you probably mean that non-hierarchical post types (such as regular old posts) are articles, rather than hierarchical ones (such as pages).

  6. Chuck Reynolds
    Member
    Posted 2 years ago #

    @Helen When we talked about that on twitter earlier it threw an error... now it's working okay... I must have typed it wrong the first time.

    Currently adding that seems to have the basic functionality I'm looking for but need/want to test it much more.

    Without 'hierarchical' set in the CPT it defaults to fasle thereby making the og:type 'article' by default. If I set 'hierarchical' => true, then og:type flips to 'website'.

    So far this seems to be working on one site I've tested it on... normal blog seems to be working and throwing 'article' on single posts and on all site pages it seems to be throwing 'website' just fine. Need to do more testing with different CPT's but I think we finally figured it out... I hope :)

Topic Closed

This topic has been closed to new replies.

About this Topic