簡體   English   中英

在循環中獲取作者的最新帖子

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

我需要在single.php顯示當前作者的最后5個帖子。 這些帖子在表wp_usermeta中注冊為meta_keyuser_posts )和meta_valuea:2:{i:0;s:4:"1336";i:1;s:4:"1334";} ),其中1336和1334是帖子ID。

我嘗試了許多方法來獲取當前作者的更多帖子,但沒有找到解決方案。

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

您可以使用以下代碼在single.php獲取當前作者的最新5篇帖子

您不需要解析該序列化的字符串

$authorID = get_the_author_meta('ID');

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

檢查wp Codex get_postsget_the_author_meta中的兩個函數doc

然后,您可以遍歷帖子以獲取各個帖子的詳細信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM