How to check for custom post type and if it's hierarchical
-
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.
- The topic ‘How to check for custom post type and if it's hierarchical’ is closed to new replies.