在WordPress中仅显示当前用户的帖子可以通过以下步骤实现:
WP_Query
类来实现这一点。以下是一个示例代码:$current_user = wp_get_current_user();
$args = array(
'author' => $current_user->ID,
'post_type' => 'post',
'posts_per_page' => -1
);
$query = new WP_Query($args);
上述代码中,wp_get_current_user()
函数用于获取当前登录用户的信息,author
参数用于指定查询的作者为当前用户的ID,post_type
参数用于指定查询的文章类型为post
,posts_per_page
参数设置为-1
表示获取所有符合条件的帖子。
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
// 在这里输出帖子的标题、内容等信息
the_title();
the_content();
}
wp_reset_postdata();
} else {
echo '当前用户没有发布任何帖子。';
}
上述代码中,$query->have_posts()
用于检查查询结果是否有帖子,$query->the_post()
用于设置当前帖子的上下文,the_title()
和the_content()
分别用于输出帖子的标题和内容。最后,使用wp_reset_postdata()
函数重置查询。
通过以上步骤,你可以在WordPress中仅显示当前用户的帖子。请注意,这只是一个简单的示例,你可以根据自己的需求进行修改和扩展。
领取专属 10元无门槛券
手把手带您无忧上云