在single.php文件中显示循环外部的作者姓名

时间:2015-07-31 18:13:08

标签: php wordpress-theming wordpress underscores-wp

我有一个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 -->

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

和另一个具有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>';}

1 个答案:

答案 0 :(得分:0)

试试这个:

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>';}