SponsoredHosinger Special Offer

WP WordPress Theme Development

Build classic and block WordPress themes from scratch: the template hierarchy, The Loop, the_title() and the_content(), template parts, menus, the Customizer, child themes and theme.json.

Dates, Authors, Categories and Tags

WordPress Theme Development Lesson 8 of 30 ~4 min read

Overview

The "posted on … by … in …" line under a post title is built from a handful of template tags for the date, author, categories, tags and comment count — and getting their output right matters for readers, accessibility and search engines.

The date

<time class="entry-date" datetime="<?php echo esc_attr( get_the_date( DATE_W3C ) ); ?>">
    <?php echo esc_html( get_the_date() ); ?>
</time>
  • get_the_date() with no argument uses the date format chosen in Settings → General, so the site owner controls how dates read.
  • The datetime attribute holds a machine-readable date, which search engines and assistive technology use.
  • Use get_the_modified_date() if you also want to show when a post was last updated.
Watch out: Prefer get_the_date() over the_date() inside a Loop. the_date() prints the date only for the first post published on a given day — the second post that day shows no date at all. It is a historic quirk, not a bug you can fix.

The author

<span class="byline">
    <?php esc_html_e( 'by', 'gdc-starter' ); ?>
    <a class="url fn n" href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ); ?>">
        <?php echo esc_html( get_the_author() ); ?>
    </a>
</span>

get_author_posts_url() links to the author archive, which renders with author.php (lesson 17).

Categories and tags

<?php
/* translators: used between list items, there is a space after the comma. */
$separator = esc_html__( ', ', 'gdc-starter' );

$categories = get_the_category_list( $separator );
if ( $categories ) {
    printf(
        '<span class="cat-links">%1$s %2$s</span>',
        esc_html__( 'Posted in', 'gdc-starter' ),
        $categories // Already escaped by WordPress.
    );
}

$tags = get_the_tag_list( '', $separator );
if ( $tags ) {
    printf(
        '<span class="tags-links">%1$s %2$s</span>',
        esc_html__( 'Tagged', 'gdc-starter' ),
        $tags // Already escaped by WordPress.
    );
}

The get_ list functions return complete, already-escaped HTML links. Each is checked before printing, so a post without tags does not show an empty "Tagged" label.

Comment count

<?php if ( ! post_password_required() && ( comments_open() || get_comments_number() ) ) : ?>
    <span class="comments-link">
        <?php
        comments_popup_link(
            esc_html__( 'Leave a comment', 'gdc-starter' ),
            esc_html__( '1 Comment', 'gdc-starter' ),
            esc_html__( '% Comments', 'gdc-starter' )
        );
        ?>
    </span>
<?php endif; ?>

Wrapping it into one reusable function

You will print this meta line on archives, single posts and search results. Writing it three times means fixing bugs three times. Put it in a function — this is a pattern every well-built theme uses. Create inc/template-tags.php:

<?php
/**
 * Custom template tags for GDC Starter.
 */

if ( ! function_exists( 'gdc_starter_posted_on' ) ) :
    /**
     * Prints the published date and author of the current post.
     */
    function gdc_starter_posted_on() {
        $time_string = sprintf(
            '<time class="entry-date" datetime="%1$s">%2$s</time>',
            esc_attr( get_the_date( DATE_W3C ) ),
            esc_html( get_the_date() )
        );

        printf(
            '<span class="posted-on">%1$s %2$s</span> <span class="byline">%3$s <a href="%4$s">%5$s</a></span>',
            esc_html__( 'Posted on', 'gdc-starter' ),
            $time_string, // Escaped above.
            esc_html__( 'by', 'gdc-starter' ),
            esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
            esc_html( get_the_author() )
        );
    }
endif;

Load it from functions.php with require get_template_directory() . '/inc/template-tags.php';, then every template simply calls <?php gdc_starter_posted_on(); ?>.

Tip: The function_exists() wrapper is deliberate. It lets a child theme define its own gdc_starter_posted_on() first, replacing yours without editing the parent — "pluggable" functions, which you will use in lesson 27.

Practice

  • Add gdc_starter_posted_on() to inc/template-tags.php, load it, and call it under the title in single.php.
  • Change the date format in Settings → General and watch your theme follow it without a code change.
  • View a post with no tags and confirm no empty "Tagged" label appears.
Create a free account to save which lessons you've finished. Save my progress