Display author name outside of loop in single.php file

I have a single.php file where I want to show author name and the date which it is posted on outside the loop, the date outputs fine but the author of the post doesn’t appear. I have tried everything like making the author name global etc.

I have three files single.php, content-single.php and a file with the function.

Read More

single.php code

get_header(); ?>

<div class="bread-crumb">
    <?php if (function_exists('dimox_breadcrumbs')) dimox_breadcrumbs(); ?>
</div>

<div class="title-main">
    <header class="entry-header">
        <?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>

            <div class="entry-meta">
                <?php motion_posted_on(); ?>
            </div><!-- .entry-meta -->
    </header><!-- .entry-header -->
</div>

<div id="primary" class="content-area">
    <main id="main" class="site-main" role="main">

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

        <?php get_template_part( 'template-parts/content', 'single' ); ?>

        <?php the_post_navigation(); ?>

        <?php
            // If comments are open or we have at least one comment, load up the comment template.
            if ( comments_open() || get_comments_number() ) :
                comments_template();
            endif;
        ?>

    <?php endwhile; // End of the loop. ?>

    </main><!-- #main -->
</div><!-- #primary -->

content-single.php code

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

<div class="entry-content">
    <?php the_content(); ?>
    <?php
        wp_link_pages( array(
            'before' => '<div class="page-links">' . esc_html__( 'Pages:', 'motion' ),
            'after'  => '</div>',
        ) );
    ?>
</div><!-- .entry-content -->

<footer class="entry-footer">
    <?php motion_entry_footer(); ?>
</footer><!-- .entry-footer -->

and another file with the related function of motion_posted_on();

function motion_posted_on() {
$time_string = '<time class="entry-date published updated" datetime="%1$s">%2$s</time>';
if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
    $time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time><time class="updated" datetime="%3$s">%4$s</time>';
}

$time_string = sprintf( $time_string,
    esc_attr( get_the_date( 'c' ) ),
    esc_html( get_the_date() ),
    esc_attr( get_the_modified_date( 'c' ) ),
    esc_html( get_the_modified_date() )
);

$posted_on = sprintf(
    esc_html_x( 'Posted on %s', 'post date', 'motion' ),
    '<a href="' . esc_url( get_permalink() ) . '" rel="bookmark">' . $time_string . '</a>'
);

$byline = sprintf(
    esc_html_x( 'by %s', 'post author', 'motion' ),
    '<span class="author vcard"><a class="url fn n" href="' . esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ) . '">' . esc_html( get_the_author() ) . '</a></span>'
);

echo '<span class="posted-on">' . $posted_on . '</span><span class="byline"> ' . $byline . '</span>';}

Related posts

1 comment

  1. Try this:

    function motion_posted_on() {
    global $post;
    $time_string = '<time class="entry-date published updated" datetime="%1$s">%2$s</time>';
    if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
        $time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time><time class="updated" datetime="%3$s">%4$s</time>';
    }
    
    $time_string = sprintf( $time_string,
        esc_attr( get_the_date( 'c' ) ),
        esc_html( get_the_date() ),
        esc_attr( get_the_modified_date( 'c' ) ),
        esc_html( get_the_modified_date() )
    );
    
    $posted_on = sprintf(
        esc_html_x( 'Posted on %s', 'post date', 'motion' ),
        '<a href="' . esc_url( get_permalink() ) . '" rel="bookmark">' . $time_string . '</a>'
    );
    
    $byline = '<a href="'.get_author_posts_url( $post->post_author).'">'. get_the_author_meta( 'display_name',$post->post_author).'</a>';
    
    echo '<span class="posted-on">' . $posted_on . '</span><span class="byline"> ' . $byline . '</span>';}
    

Comments are closed.