I need to add the date and author to blog posts. The line needs to read like this example:

Posted on January 20, 2018 by Name

Clicking on the date will take you to a page with all posts published on that date. Clicking on the Name/author will take you to a page with all posts by that author.

I did some research and was able to find code to add to the single.php template.

<div class="sample">Posted on <?php the_date(); ?> by <?php 
    the_author(); ?></div>

I will need to create a class for "sample". How do I get the date and author to link where they need to go?

Here is the code for my single.php file:

    * The Template for displaying all single posts.
    * @package WordPress
    * @subpackage Cornerstone
    * @since Cornerstone 1.0

    get_header(); ?>

    <div class="row">
    <div class="small-12 medium-12 columns">

            <h1 class="entry-title"><?php the_title(); ?></h1>
            <div class="date">Posted on <?php the_date(); ?> by <?php 
    the_author(); ?></div>          
            <div class="breadcrumbs" xmlns:v="http://rdf.data- 
                 <?php if(function_exists('bcn_display')){bcn_display();}?>


    <div class="row">
    <div id="primary" class="site-content small-12 medium-8 columns">
    <div id="content" role="main">

        <?php do_action( 'cornerstone_before_content' ); ?>

        <?php while (have_posts()) : the_post(); ?>

            <?php get_template_part( 'content', get_post_format() ); ?>

        <?php endwhile; ?>

        <?php do_action( 'cornerstone_after_content' ); ?>


    <?php get_sidebar(); ?>


    <?php get_footer(); ?>
  • Page template code added to question. – Webman Aug 8 '18 at 13:54

Use the following code in file template single.php (https://developer.wordpress.org/reference/functions/get_day_link/#user-contributed-notes ) :

 $archive_year  = get_the_time( 'Y' );
 $archive_month = get_the_time( 'm' );
 $archive_day   = get_the_time( 'd' );
 $month= get_the_date('M');
<div class="custom_archives">
    Posted on: <a href="<?php echo esc_url( get_day_link( $archive_year, $archive_month, $archive_day ) ); ?>"><?php echo $month. " ".$archive_day.", " .$archive_year; ?>
    <a class="author-link" href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ); ?>" rel="author"><?php printf( __( 'View all posts by %s', 'twentysixteen' ), get_the_author() ); ?>

The first link is the daily archive, the second the author archive

  • Please forgive my ignorance, but when I add this code to the single.php file, the blog/posts page does not work anymore. – Webman Aug 8 '18 at 15:38
  • try now, there was an extra white space in the code – Andrea Somovigo Aug 8 '18 at 16:26
  • The code is now working! Instead of showing the date as: 2018-08-04, I would need it to show as August 4, 2018 – Webman Aug 8 '18 at 16:52
  • see again the updated answer – Andrea Somovigo Aug 8 '18 at 20:17
  • Perfect! Thanks for your help....much appreciated! – Webman Aug 8 '18 at 21:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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