簡體   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