Question

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(); ?>
Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top