为了账号安全,请及时绑定邮箱和手机立即绑定

仅当由当前登录的作者 Wordpress 撰写时才显示单个帖子

仅当由当前登录的作者 Wordpress 撰写时才显示单个帖子

PHP
繁华开满天机 2023-11-05 15:25:55
我发现了一些代码,将帖子提要限制为下面当前登录的作者,效果很好。现在,我正在寻找一种方法将相同的限制应用于单个帖子。我会在代码中调整什么,以便它可以在单个.php上运行?如果我现在按原样使用此代码,它会生成一个提要而不是单个帖子。<?php if (is_user_logged_in()):        global $current_user;        wp_get_current_user();        $author_query = array('author' => $current_user->ID);        $author_posts = new WP_Query($author_query);        while($author_posts->have_posts()) : $author_posts->the_post(); ?>
查看完整描述

1 回答

?
精慕HU

TA贡献1845条经验 获得超8个赞

您可以将帖子 ID 与作者一起传递,例如WP_Query


$args = array('author' => $current_user->ID,

              'p' => get_the_ID() );

$author_posts = new WP_Query( $args );

但是,这似乎您正在做不需要的额外工作 - 您已经在帖子页面上,因此已经设置了循环以显示帖子详细信息。


我假设您要做的是检查它是否是登录用户自己的帖子,并且仅在是时显示它。在这种情况下,您需要做的就是:


$current_user = wp_get_current_user();

if (is_user_logged_in() && $current_user->ID == $post->post_author)  {

    /* the user is logged in and the current logged-in user is the post author */      

    /* Show the post here....*/

}

else{


    /* Author is not the logged in user!

       Do whatever you want in this case...*/

}


查看完整回答
反对 回复 2023-11-05
  • 1 回答
  • 0 关注
  • 64 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信