5

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".

11

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;
}
  • 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
1
<?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.

  • 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. – Butuzov Jan 31 '13 at 6:19
0

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() ?>

0

I'm a little too late but a better and small solution using array filter would be:

function getPage( $arrayIn ) {
    $prefix = 'is_';

    $array = array_filter( array_filter( (array) $arrayIn, function ( $key ) use ( $prefix ) {
        return ( strpos( $key, $prefix ) === 0 );
    }, ARRAY_FILTER_USE_KEY ) );

    unset( $array['is_singular'] );

    return preg_replace( '/^' . preg_quote( $prefix, '/' ) . '/', '', key( $array ) );
}

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.