1

I want to make function, which gets me a post type slug (or object - doesn't matter) of post type displaying in Archive page, or even on archive pages of its taxonomies. Is it possible?

I have this conditional function to recognize a post type (archive / single), but I would like it to return the name, if it's not set:

function rb_oftype($type = false) {
global $post;

if ( !$type ){
    //get the type [post_type,tax]


}

if ( count($post) > 0 ){
    //if is of post type

    if ( $post->post_type === $type ) return true;

}
elseif ( is_archive() ) {
    //added also for post type archives

    return is_post_type_archive( $type );

}
elseif ( is_tax() ){
    //also for all custom post taxonomy archives

    $taxonomies = (array) get_object_taxonomies($type);

    $keys = array_keys( $taxonomies );
    for ($i=0;$i<$keys;$i++)  if ( is_tax( $taxonomies[$keys[$i]] ) ) 

return true;

    }

    return false;
}

Can anybody help me with that? Thx

1

Use get_queried_object to get information about the current page.

  • if you're on a single post, it will return the post object
  • if you're on a page, it will return the page object
  • if you're on an archive page, it will return the post type object
  • if you're on a category archive, it will return the category object
  • if you're on an author archive, it will return the author object
  • etc.
  • Thanks, it works pretty good now. I was also able to simplify and improve the function (and condition) a bit – user1980807 Sep 20 '15 at 15:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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