我试图在我的网站主页上显示我的最新3篇博客文章。到目前为止,我已经掌握了一切(内容、图像和日期)。然而,它不允许我用链接显示作者的名字,这样人们就可以点击作者并查看他们的最新帖子。有人知道为什么会这样吗?
到目前为止我的代码:
<div class="col-sm-6">
<?php
$args = array( 'numberposts' => 2, 'offset' => 1, 'post_status'=>"publish",'post_type'=>"post",'orderby'=>"post_date");
$postslist = get_posts( $args );
foreach ($postslist as $post) : setup_postdata($post);
?>
<div class="blog-post blog-media wow fadeInRight" data-wow-duration="300ms" data-wow-delay="100ms">
<article class="media clearfix">
<div class="entry-thumbnail pull-left">
<div class="img-responsive smallBlogImage"><?php getTheFirstImage(); ?></div>
<span class="post-format post-format-gallery"><img class="blogIconSmall" src="IsosecWeb/images/IsosecIcon.png"></span>
</div>
<div class="media-body">
<header class="entry-header">
<div class="entry-date"><?php the_date(); ?></div>
<h2 class="entry-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
</header>
<div class="entry-content">
<p><?php echo wp_trim_words(preg_replace("/\< *[img][^\>]*[.]*\>/i","", get_the_content(), 80), 20); ?></p>
<a class="btn btn-primary" href="<?php the_permalink(); ?>">Read More</a>
</div>
<footer class="entry-meta">
<span class="entry-author"><i class="fa fa-pencil"></i> <?php echo the_author_posts_link(); ?> </span>
</footer>
</div>
</article>
</div>
<?php endforeach; ?>
</div>发布于 2015-07-24 11:53:25
解释
之所以没有出现这种情况,是因为the_author_posts_link();需要在环路中使用,而只需要在循环中使用。
如果您print_r($postslist),您将得到一个数组,如下所示:
Array ( [0] => Array ( [ID] => 1 [post_author] => 1 [post_date] => 2015-07-03 11:04:24 [post_date_gmt] => 2015-07-03 11:04:24 [post_content] => Welcome to WordPress. This is your first post. Edit or delete it, then start blogging! [post_title] => Hello world! [post_excerpt] => [post_status] => publish [comment_status] => open [ping_status] => open [post_password] => [post_name] => hello-world [to_ping] => [pinged] => [post_modified] => 2015-07-19 21:11:24 [post_modified_gmt] => 2015-07-19 21:11:24 [post_content_filtered] => [post_parent] => 0 [guid] => http://localhost:8888/intelligence/?p=1 [menu_order] => 0 [post_type] => post [post_mime_type] => [comment_count] => 1 [filter] => raw ) )您需要做的是单独访问键值。例如,要获得日期,请按如下方式访问:
<?php echo postslist['post_date']; ?>作者链接
您将注意到,如果您查看post_author键,它将返回一个整数值,或管理员的id。我们能做的就是把这个传递到
<span class="entry-author">
<i class="fa fa-pencil"></i>
<?php the_author_meta( 'display_name', $postslist['post_author'] ); ?>
</span>您可以访问作者链接如下:
<a href="<?php echo get_author_posts_url( $postslist['post_author'] ); ?>">
Posts by <?php the_author_meta( 'display_name', $postslist['post_author'] ); ?>
</a>发布于 2015-07-24 12:09:13
试试这个:
<a href="<?php echo get_author_posts_url( $post->post_author); ?>"><?php echo get_the_author_meta( 'display_name',$post->post_author); ?></a>发布于 2015-07-24 13:51:01
你的代码看起来很正确。函数"the_author_posts_link()“返回锚标记,该标记链接到作者发布的所有帖子。没有必要写“回声”来打印它。
你只需要替换
<?php echo the_author_posts_link(); ?> 使用
<?php the_author_posts_link(); ?>它会成功的。
https://stackoverflow.com/questions/31609004
复制相似问题