如何仅为特定用户ID显示类

时间:2016-11-10 19:26:55

标签: wordpress

我有以下代码行,其中包含编辑附件的链接。我只希望某些用户ID能够看到此编辑链接。如何将此链接(class = edit-attachment)显示为特定用户ID?

<div class="details">
            <div class="filename">29982792291_9e1fc5b238_k-1.jpg</div>
            <div class="uploaded">November 9, 2016</div>
            <div class="file-size">1 MB</div>
            <div class="dimensions">2048 × 1365</div>
            <a class="edit-attachment" href="https://www.citynarrative.com/wp-admin/post.php?post=308&amp;action=edit&amp;image-editor" target="_blank">Edit Image</a>
            <button type="button" class="button-link delete-attachment">Delete Permanently</button>
            <div class="compat-meta">
            </div>
        </div>

2 个答案:

答案 0 :(得分:0)

取决于您谈论用户ID时的含义。

您可以使用edit_post_link( $link, $before, $after, $id, $class );函数,其中$ id是帖子(或附件)ID

这只会根据用户资格显示链接。

更多详情edit_post_link

$user = wp_get_current_user();
$allowed_roles = array('editor', 'administrator', 'author'); // can be authorized ids 
// but you'll need to replace $user->roles by $user->ID in array_intersect

   if( array_intersect($allowed_roles, $user->roles ) ) {  ?> 
        <div class="details">
            <div class="filename">29982792291_9e1fc5b238_k-1.jpg</div>
            <div class="uploaded">November 9, 2016</div>
            <div class="file-size">1 MB</div>
            <div class="dimensions">2048 × 1365</div>
            <?php echo edit_post_link( __('Edit Image', text-domain), '', '', $id );?>
            <button type="button" class="button-link delete-attachment">Delete Permanently</button>
            <div class="compat-meta">
            </div>
       </div>
   <?php } ?>

您还可以检查用户是否已登录及其角色,以显示与current_user_can()

的链接

答案 1 :(得分:0)

试试此代码

$user_array = array(1, 2 , 3 );  // list of user id
$user_id = get_current_user_id(); // login user id
if (in_array($user_id, $user_array))
{
    ?>
     <div class="details">
            <div class="filename">29982792291_9e1fc5b238_k-1.jpg</div>
            <div class="uploaded">November 9, 2016</div>
            <div class="file-size">1 MB</div>
            <div class="dimensions">2048 × 1365</div>
            <a class="edit-attachment" href="https://www.citynarrative.com/wp-admin/post.php?post=308&amp;action=edit&amp;image-editor" target="_blank">Edit Image</a>
            <button type="button" class="button-link delete-attachment">Delete Permanently</button>
            <div class="compat-meta">
            </div>
        </div>
    <?php
}