I need to output the name of the custom post type within archive.php and single.php.
I would use something like this:
if ( 'staff' == get_post_type() ) {
$post_type = 'staff';
}
echo '<div class="'.$post_type.'">Content</div>';
This would give me the required result of <div class="staff">
However, it appears I can also access the global variable called $post_type
and simply output this on archive.php or single.php even without declaring global
, for example this archives the same result as above:
<div class="<?php echo $post_type; ?>">
Codex says the global variable $post_type
is for use within the Wordpress admin area.
My questions are:
- Why does
$post_type
work without declaring it globally first? - Is it a good idea to use the
$post_type
global var as demonstrated above for use in theme templates?
Thank you