我正在尝试创建一个类似于仪表板的自定义页面模板,该模板列出当前登录用户的帖子。我试图在网上找到一个解决方案。但都不合适
如何在自定义页面模板中按当前用户/作者显示帖子?
2 个回复
最合适的回答,由SO网友:Milo 整理而成
这应该适合您:
if ( is_user_logged_in() ):
global $current_user;
wp_get_current_user();
$author_query = array(\'posts_per_page\' => \'-1\',\'author\' => $current_user->ID);
$author_posts = new WP_Query($author_query);
while($author_posts->have_posts()) : $author_posts->the_post();
?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
<?php
endwhile;
else :
echo "not logged in";
endif;
SO网友:Alvin
将自定义发布时间包括在$author_query array
, 添加另一个key=>value
元素到$author_query
大堆
示例:
$author_query = array(
\'posts_per_page\' => \'-1\',
\'author\' => $current_user->ID,
\'post_type\'=>\'your custom post type name\'
);
结束