简体   繁体   English

在循环中获取作者的最新帖子

[英]Get the latest posts of the author in the loop

I need to display the last 5 posts for current author in single.php . 我需要在single.php显示当前作者的最后5个帖子。 These posts are registered in the table wp_usermeta as a meta_key ( user_posts ) and meta_value ( a:2:{i:0;s:4:"1336";i:1;s:4:"1334";} ) where 1336 and 1334 are post ids. 这些帖子在表wp_usermeta中注册为meta_keyuser_posts )和meta_valuea:2:{i:0;s:4:"1336";i:1;s:4:"1334";} ),其中1336和1334是帖子ID。

I've tried many methods to get more posts of current author, and found no solution. 我尝试了许多方法来获取当前作者的更多帖子,但没有找到解决方案。

<?php
 $post_ids = get_user_meta($user_id,'user_posts',true); 
 $posts = get_posts(array(
 'author' => get_the_author_id(), 
 'numberposts' => 5,  
 'orderby' => 'post_date', 
 'post__in' => explode(',', $post_ids)));
  if($posts) { echo '<ul>';
    foreach($posts as $post) { ?>
                <li><a href="<?php echo get_permalink($p->ID) ?>" rel="bookmark"  title="Permanent Link to <?php echo $post->post_title; ?>"><?php echo $post->post_title; ?> </a></li>
    <?php }
    echo '</ul>';} ?>

You can use the following code to fetch the latest 5 posts by current author in single.php 您可以使用以下代码在single.php获取当前作者的最新5篇帖子

You dont need to parse that serialized string 您不需要解析该序列化的字符串

$authorID = get_the_author_meta('ID');

$authors_posts = get_posts( array( 
     'author' => $authorID, 
     'numberposts' => 5,
     'orderby' => 'date'      
   ) 
 );

Check the two functions doc in wp codex get_posts and get_the_author_meta 检查wp Codex get_postsget_the_author_meta中的两个函数doc

You can then loop over the posts to get individual post details. 然后,您可以遍历帖子以获取各个帖子的详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM