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.

I would like to display a custom subtitle for the different type of archive pages. I can easily know what type of archive page I'm in (date, category, etc.) but I have found nothing online that tells me how to get meaningful information for each of the specific, archive page types.

I have the following function:

function get_the_subtitle() {
    global $post;
    $subtitle = '';
    if (is_archive()) {
        $subtitle = 'Blog posts for ';
    }

    if (is_date()) {
        $subtitle .= '2012/03';       // Real archive date here of current page
    } else if (is_category()) {
        $subtitle .= 'Uncategorized'; // Real archive category here of current page
    } else if (is_author()) {
        $subtitle .= 'admin';         // Real archive author here of current page
    } else if (is_tag()) {
        $subtitle .= 'tag';           // Real archive tag here of current page
    }
    return $subtitle;
}

Can someone please show me how to easily create the different subtitles for the specific archive pages? Filling in this function with the means to create the values would be most beneficial.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

How about using the single_cat_title() function regardless of what type of archive it is? For example:

function get_the_subtitle() {
    if( is_archive() ) {
        $subtitle = 'Blog posts for ';
    }

    if( is_category() || is_tag() ) {
        $subtitle .= single_cat_title( '', false );
    }
    if( is_author() ) {
        $curauth = ( get_query_var( 'author_name' ) ) ?
            get_user_by( 'slug', get_query_var( 'author_name' ) ) :
            get_userdata( get_query_var( 'author' ) );
        // you can set this to many things, see the get_userdata() docs for a list
        $subtitle .= $curauth->user_nicename;
    }
    if( is_date() ) {
        $month = get_query_var('monthnum')
        $day   = get_query_var('day')
        $year  = get_query_var('year')

        $string = '';

        $string .= ( !empty( $day ) )    ? $day   : '';
        $string .= ( !empty( $string ) ) ? '/'    : '';
        $string .= ( !empty( $month ) )  ? $month : '';
        $string .= ( !empty( $string ) ) ? '/'    : '';
        $string .= ( !empty( $year ) )   ? $year  : '';

        $subtitle .= $string;
    }

    if( !empty( $subtitle ) ) {
        return $subtitle;
    } else {
        return false;
    }
}
share|improve this answer
    
This doesn't give me anything for the author or the date. What would I use for those archive pages? –  Stephen Watkins Mar 18 '12 at 1:46
    
Dang, wish it was that easy...oh well, try the updated code on for size. –  m0r7if3r Mar 18 '12 at 2:10

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.