如何仅向用户显示登录用户的帖子?

时间:2016-02-23 11:17:15

标签: wordpress

我想创建一个用户只能看到自己帖子的网站。

注意:我不是在谈论管理部分。我想限制在面向公众的网站上显示的帖子只显示属于该登录用户的帖子。

如果他们没有登录,他们会看到页面但没有帖子。如果他们已登录但没有任何帖子,则页面也将为空白。但如果他们登录已创建的帖子,那么这些帖子将在相关页面上显示。

这可能吗?已经是wp的一个功能,但我还没有看到它?

2 个答案:

答案 0 :(得分:1)

在模板中使用custom query

{"excount":2 "ex1":{"status":1}"ex2":{"status":2}}

答案 1 :(得分:0)

您可以通过以下代码获取登录用户的帖子。

<?php 

$user = wp_get_current_user();
$usatual = $user->user_login;
$usemail = $user->user_email;
echo 'Posts from user:'.$usatual.', e-mail: '.$usemail.'<BR><BR>';

$query_args = array(
    'post_type' => 'post',
    'author' => $user->ID,
);
$query = new WP_Query($query_args);
echo '<table><tr>';
if ($query->have_posts()) {
    while ($query->have_posts()) {
        $query->the_post();
        $post = $query->post;
        $title = $post->post_title;
        $content = $post->post_content;
        echo '<tr><td>' . $title . '</td>';
        echo '<td>' . $content . '</td></tr>';
    }
    echo '</tr></table>';
    wp_reset_postdata();
}
else{
    echo 'No posts found.';
}

?>