我發現了一些代碼,將帖子提要限制為下面當前登錄的作者,效果很好。現在,我正在尋找一種方法將相同的限制應用于單個帖子。我會在代碼中調整什么,以便它可以在單個.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...*/
}
- 1 回答
- 0 關注
- 179 瀏覽
添加回答
舉報
0/150
提交
取消