获取特定作者的所有帖子 - wordpress

问题描述 投票:0回答:1

我需要获取特定作者创建的所有帖子。例如,作者“test”创建的所有帖子。对于每个帖子,我都需要帖子标题、描述和特色图片。谁能帮忙?

if(isset($authors) && !empty($authors))
{
    echo "<ul>";
    foreach($authors as $author)
    {
        $posts = get_posts(array('test'=>$author->ID));
    }
}
wordpress posts custom-code
1个回答
0
投票

你可以这样做

$args = array(
'author_name' => 'test', // Replace 'test' with the actual author name
'posts_per_page' => -1 // -1 means retrieve all posts
);

$query = new WP_Query($args);

 if ($query->have_posts()) {
  while ($query->have_posts()) {
     $query->the_post();
     $post_title = get_the_title();
     $post_description = get_the_excerpt();
     $post_featured_image = get_the_post_thumbnail_url();
    
     // Output the post information as needed
     echo '<h2>' . $post_title . '</h2>';
     echo '<p>' . $post_description . '</p>';
     echo '<img src="' . $post_featured_image . '" alt="' . $post_title . '">';
    }
 }
wp_reset_postdata();
© www.soinside.com 2019 - 2024. All rights reserved.