我的 WordPress 网站使用网站成员提交的帖子(实际上是 CPT)。显示 CPT 帖子时,我希望在作者查看帖子时显示“编辑”和“删除”按钮,而在任何其他成员查看帖子时则不显示按钮。 我研究了代码和插件,但所有这些似乎只适用于作者 Angular 色,而不适用于特定作者本身。 有没有办法使用 PHP 函数/短代码、Javascript 或两者的某种组合来添加此功能?
最佳答案
这是禁用编辑和删除按钮的代码。我已经测试了代码,在我这边工作得很好。
仅适用于编辑按钮链接
function prefix_remove_get_edit_post_link( $link ) {
global $post;
$current_user = get_current_user_id();
$author_id = get_post_field ('post_author', $post_id);
if($author_id == $current_user) {
return $link;
}
return null;
}
add_filter('get_edit_post_link', 'prefix_remove_get_edit_post_link');
仅适用于删除按钮链接
function prefix_remove_get_delete_post_link( $link ) {
global $post;
$current_user = get_current_user_id();
$author_id = get_post_field ('post_author', $post_id);
if($author_id == $current_user) {
return $link;
}
return null;
}
add_filter('get_delete_post_link', 'prefix_remove_get_delete_post_link');
对于隐藏按钮编辑和删除
add_filter( 'post_row_actions', 'remove_row_actions', 10, 1 );
function remove_row_actions( $actions )
{
global $post;
$current_user = get_current_user_id();
$author_id = get_post_field ('post_author', $post_id);
if($author_id != $current_user) {
if( get_post_type() === 'post' ){
unset( $actions['edit'] );
unset( $actions['trash'] );
unset( $actions['inline hide-if-no-js'] );
}
}
return $actions;
}
关于javascript - WordPress 仅在帖子作者时才显示编辑和删除按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68463099/