0

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:

    <?php
    /**
    * 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- 
    vocabulary.org/#">
                 <?php if(function_exists('bcn_display')){bcn_display();}?>
            </div>

    </div>
    </div>

    <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' ); ?>

    </div>
    </div>

    <?php get_sidebar(); ?>

    </div>

    <?php get_footer(); ?>
2
  • Page template code added to question.
    – Webman
    Commented Aug 8, 2018 at 13:54
  • I’m also looking for the same solution to add the post date at the bottom of the article. My website is hosted on WordPress. Commented Feb 1, 2021 at 6:43

1 Answer 1

2

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

 <?php
 $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">
  <p>
    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>
  </p>
  <p>
    <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() ); ?>
    </a>
  </p>
</div>

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

5
  • Please forgive my ignorance, but when I add this code to the single.php file, the blog/posts page does not work anymore.
    – Webman
    Commented Aug 8, 2018 at 15:38
  • try now, there was an extra white space in the code Commented Aug 8, 2018 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
    Commented Aug 8, 2018 at 16:52
  • see again the updated answer Commented Aug 8, 2018 at 20:17
  • Perfect! Thanks for your help....much appreciated!
    – Webman
    Commented Aug 8, 2018 at 21:03

Your Answer

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

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