When I query the category (category.php), how do I get the title of the current category, ie., the one that is being queried?
And how do I get the title for tag and date (be it the day, the month or the year)?
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>
single_tag_title
is responsible for echoing. if you pass true
, then it will be displayed. if false
then it will be returned.
Commented
Feb 20, 2014 at 7:08
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:
single_post_title()
for postsget_the_date()
for date based archivesTry 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.
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; >
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>';
?>