2

我有一个 single.php 文件,我想在其中显示作者姓名和在循环外发布的日期,日期输出正常,但帖子的作者没有出现。我已经尝试了一切,比如让作者的名字成为全球性的等等。

我有三个文件 single.php、content-single.php 和一个包含函数的文件。

single.php 代码

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 -->

内容-single.php 代码

<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 -->

和另一个具有相关功能的文件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>';}
4

1 回答 1

1

尝试这个:

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>';}
于 2015-08-01T06:16:08.920 回答