admin管理员组文章数量:1419573
I am creating an author.php page and listing all the posts from an author. Although admins can see the posts' edit links i want to echo the link if logged in user is the current user
for example
if testuser is logged in and current page is /author/testuser he can see edit post links
but
if testuser is logged in and current page is /author/theee he cant see the links
currently i have
$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
$th = $curauth->nickname;
$cu = $current_user->user_login;
if ( $th = $curauth ) {
edit_post_link('edit', '', '');
} else {
}
but still only admins can see the links.
I am creating an author.php page and listing all the posts from an author. Although admins can see the posts' edit links i want to echo the link if logged in user is the current user
for example
if testuser is logged in and current page is /author/testuser he can see edit post links
but
if testuser is logged in and current page is /author/theee he cant see the links
currently i have
$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
$th = $curauth->nickname;
$cu = $current_user->user_login;
if ( $th = $curauth ) {
edit_post_link('edit', '', '');
} else {
}
but still only admins can see the links.
Share Improve this question edited Feb 9, 2013 at 15:46 Emre Caglar asked Feb 9, 2013 at 15:42 Emre CaglarEmre Caglar 511 gold badge1 silver badge4 bronze badges 5 |4 Answers
Reset to default 8If you just have to modify the author.php page, this piece of code will probably work :
<?php
if( is_user_logged_in() && is_author(get_current_user_id()) ) {
edit_post_link('edit', '', '');
}
?>
The first part of the conditions checks if there is a user logged. The second one will be true if the current page is the author page of the current user.
I think the post edit link should be visible to post author and moderator(editor user or how have that capability)
So my proposed code is like this
global $post;
$current_user = wp_get_current_user();
if(current_user_can( 'edit_others_posts', $post->ID ) && ($post->post_author == $current_user->ID)) {
//show edit link
}
Placed in your theme's functions.php
this should globally alter the behavior of edit_post_link
. It should work on all of your archives, not just your author archives-- anything that uses edit_post_link
.
function limit_edit_Link_wpse_85214($link) {
global $post,$current_user;
get_currentuserinfo();
if ($post->post_author == $current_user->ID) {
return $link;
}
return false;
}
add_filter('get_edit_post_link','limit_edit_Link_wpse_85214');
If will only work reliably inside a Loop, which is the only place you should be using edit_post_link
anyway.
If you want this to work only for your author archive pages, just paste that code before the Loop in the author.php
template and don't put it in function.php
. It shouldn't effect anything but that page then. Or you can just use the guts for the function to make your conditional, like you were trying to do.
I could not get the @Mat solution to work. It looks correct, however.
WP stores the value of $post->post_author as a string. However, get_the_author_meta( 'ID' )
and get_current_user_id()
each return an integer, so strict comparison works in the second part of the IF statement.
If you replace get_the_author_meta( 'ID' )
with $post->post_author
you must use ==
and not ===
as the comparison operator because the value of $post->post_author is a string and get_current_user_id()
returns an integer.
Test:
<?php echo 'Author ID type: ' . gettype( get_the_author_meta( 'ID' ) ) . '<br>'; ?>
<?php echo 'Current user ID type: ' . gettype( get_current_user_id() ); ?>
<?php
echo 'Is logged in user the author? ';
if ( get_the_author_meta( 'ID' ) == get_current_user_id() ) {
echo 'TRUE <br>';
} else {
echo 'FALSE';
}
?>
Solution using strict comparison operator that works for me:
<?php if ( is_user_logged_in() && get_the_author_meta( 'ID' ) === get_current_user_id() ) : ?>
//...
<?php endif; ?>
本文标签: Display edit link if post author is current user
版权声明:本文标题:Display edit link if post author is current user 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745317510a2653219.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
edit_post_link()
? Then everything works as expected, for all users, at any level (Author, Editor, Administrator, etc). – Pat J Commented Mar 19, 2013 at 23:26