0

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.

1 Answer 1

2

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;
    }
}
2
  • This doesn't give me anything for the author or the date. What would I use for those archive pages? Mar 18, 2012 at 1:46
  • Dang, wish it was that easy...oh well, try the updated code on for size.
    – mor7ifer
    Mar 18, 2012 at 2:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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