繁体   English   中英

Wordpress 仅在帖子作者时显示编辑和删除按钮

[英]Wordpress show Edit and Delete buttons only if post's author

我的 wordpress 站点使用站点成员提交的帖子(实际上是 CPT)。 在显示 CPT 帖子时,我希望在作者查看帖子时显示用于编辑和删除的按钮,并且在任何其他成员查看帖子时不显示。 我研究过代码和插件,但似乎都只适用于作者角色,而不是特定作者本身。 有没有办法使用 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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM