Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Is there a function that simply returns the current "page type" instead of using is_page(), is_preview(), is_single(), is_archive(), etc?

For example: I can find the current "post type" but I can't find it's corresponding "page type".

share|improve this question

3 Answers 3

up vote 1 down vote accepted

You need your own helper function which will return you what you need. It could be like this one:

function wpse8170_loop() {
    global $wp_query;
    $loop = 'notfound';

    if ( $wp_query->is_page ) {
        $loop = is_front_page() ? 'front' : 'page';
    } elseif ( $wp_query->is_home ) {
        $loop = 'home';
    } elseif ( $wp_query->is_single ) {
        $loop = ( $wp_query->is_attachment ) ? 'attachment' : 'single';
    } elseif ( $wp_query->is_category ) {
        $loop = 'category';
    } elseif ( $wp_query->is_tag ) {
        $loop = 'tag';
    } elseif ( $wp_query->is_tax ) {
        $loop = 'tax';
    } elseif ( $wp_query->is_archive ) {
        if ( $wp_query->is_day ) {
            $loop = 'day';
        } elseif ( $wp_query->is_month ) {
            $loop = 'month';
        } elseif ( $wp_query->is_year ) {
            $loop = 'year';
        } elseif ( $wp_query->is_author ) {
            $loop = 'author';
        } else {
            $loop = 'archive';
        }
    } elseif ( $wp_query->is_search ) {
        $loop = 'search';
    } elseif ( $wp_query->is_404 ) {
        $loop = 'notfound';
    }

    return $loop;
}
share|improve this answer
    
Yeah, I was hoping WP had something built-in to handle this. Unfortunately not! Thanks for the function! –  EddyR Jan 31 '13 at 10:04
<?php
var_dump(get_query_var('post_type'));

but even than that page can be a single or archive or anything else...

What you tring to achive? Perhaps in that case will be more understand what you need.

update. take a look to sample theme function that you can use to achive this task. function called twentyeleven_body_classes from Twentyeleven theme. it returns clases that can be used for body_classes filter

function twentyeleven_body_classes( $classes ) {

    if ( function_exists( 'is_multi_author' ) && ! is_multi_author() )
        $classes[] = 'single-author';

    if ( is_singular() && ! is_home() && ! is_page_template( 'showcase.php' ) && ! is_page_template( 'sidebar-page.php' ) )
        $classes[] = 'singular';

    return $classes;
}

nothing holding you from not adding to it additional classes. is_archive() or something else... in this case you got a page types you want.

share|improve this answer
    
What I want to know is if the page is single, archive, attachment, etc. I don't care about the post type. –  EddyR Jan 31 '13 at 6:10
    
In this take as example on of the body_classes functions. one of tham given in update code. –  Oleg Butuzov Jan 31 '13 at 6:19

If you place this template tag within the Body tag <?php body_class(); ?> It will output it in your code i.e. <body <?php body_class(); ?>> That will give you the templates referenced for the given page.

Alternatively you could echo the path to the page template being called i.e. page.php or single-posttype.php by using this: <?php echo get_page_template() ?>

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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