显示作者链接(WordPress)

时间:2015-07-24 11:07:59

标签: php wordpress

我试图在我的网站主页上显示我最新的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>

3 个答案:

答案 0 :(得分:1)

试试这个:

 <a href="<?php echo get_author_posts_url( $post->post_author); ?>"><?php echo get_the_author_meta( 'display_name',$post->post_author); ?></a>

答案 1 :(得分:0)

<强>解释

之所以没有显示,是因为the_author_posts_link();需要在The Loop中使用而且只需要循环使用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

作者链接

您会注意到,如果查看<span class="entry-author"> <i class="fa fa-pencil"></i> <?php the_author_meta( 'display_name', $postslist['post_author'] ); ?> </span> 键,它会返回一个整数值,或者是admin的ID。我们能做的就是将其传递给

<a href="<?php echo get_author_posts_url( $postslist['post_author'] ); ?>">
    Posts by  <?php the_author_meta( 'display_name', $postslist['post_author'] ); ?>
</a>

您可以这样访问作者链接:

<documentCache class="solr.FastLRUCache" size="40960" initialSize="40960"
  autowarmCount="0" cleanupThread="true"/>

答案 2 :(得分:0)

您的代码看起来非常正确。函数“the_author_posts_link()”返回锚标记 - 链接到作者的所有帖子。没有必要写“echo”来打印它。

您只需要替换

<?php echo the_author_posts_link(); ?> 

<?php the_author_posts_link(); ?>

它会起作用。