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.

When I query the category (category.php), how to get the name of the current category, ie., the one that is being queried?

And for tag and date (be it the day, the month or the year)?

Thanks!!

share|improve this question
add comment

5 Answers

For category use single_cat_title function:
http://codex.wordpress.org/Function_Reference/single_cat_title

For tag use single_tag_title function:
http://codex.wordpress.org/Function_Reference/single_tag_title

For date use get_the_date function:
http://codex.wordpress.org/Function_Reference/get_the_date

For example if you open twentyten theme you will see following:

category.php:

<h1 class="page-title"><?php
    printf( __( 'Category Archives: %s', 'twentyten' ), '<span>' . single_cat_title( '', false ) . '</span>' );
?></h1>

date.php:

<h1 class="page-title">
    <?php if ( is_day() ) : ?>
        <?php printf( __( 'Daily Archives: <span>%s</span>', 'twentyten' ), get_the_date() ); ?>
    <?php elseif ( is_month() ) : ?>
        <?php printf( __( 'Monthly Archives: <span>%s</span>', 'twentyten' ), get_the_date( _x( 'F Y', 'monthly archives date format', 'twentyten' ) ) ); ?>
    <?php elseif ( is_year() ) : ?>
        <?php printf( __( 'Yearly Archives: <span>%s</span>', 'twentyten' ), get_the_date( _x( 'Y', 'yearly archives date format', 'twentyten' ) ) ); ?>
    <?php else : ?>
        <?php _e( 'Blog Archives', 'twentyten' ); ?>
    <?php endif; ?>
</h1>
share|improve this answer
    
does single_tag_title 'get' the tag title or does it echo it? –  henrywright Feb 19 at 23:23
    
the second parameter of single_tag_title is responsible for echoing. if you pass true, then it will be displayed. if false then it will be returned. –  Eugene Manuilov Feb 20 at 7:08
add comment

In addition to the other answers, you can call: single_term_title('Currently browsing: ') to display 'Currenty browsing term' (where term is the name of the taxonomy term you are viewing. (See Codex)

This works for custom taxonomies as well category and tag terms.

Also, you might find it easier to use wp_title which handles taxonomies and archives, displaying the appropriate title depending on what you are viewing. It essentially switches through all the available functions for displaying titles so you might want to take a look at the source code. Others include:

share|improve this answer
add comment

Try the following

<?php single_cat_title(); ?>
<?php single_tag_title(); ?>
<?php the_time('F jS, Y'); ?> // day, month, year
<?php the_time('F, Y'); ?> // month, year
<?php the_time('Y'); ?> // year

See the Codex for more on formatting the date: HERE

PS. These are to be called within the loop. Except the first two which must be outside the loop.

share|improve this answer
add comment

Thank you for your replies! I made this for date:

for archive.php

<?php
/*get archives header*/
if ( is_day() ) { $this_header = "Daily archives for " . get_the_date(); }
else if ( is_month() ){ $this_header = "Monthly archives for " . get_the_date('F, Y'); }
else if ( is_year() ){ $this_header = "Yearly archives for " . get_the_date('Y'); }
else { $this_header = "Archives"; }
?>

then just

<?php echo $this_header; >
share|improve this answer
add comment

This is probably more than you need right now, but is probably something you will need on other areas of your theme.

This code gets the category name of the current post, then displays it as a link to the posts listed in the category via the category.php file.

<?php
$category = get_the_category();
$current_category = $category[0];
$parent_category = $current_category->category_parent;
if ( $parent_category != 0 ) {
echo '<a href="' . get_category_link($parent_category) . '">' . get_cat_name($parent_category) . '</a>';
}
echo '<a href="' . get_category_link($current_category) . '">' . $current_category->cat_name . '</a>';
?>
share|improve this answer
add comment

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.