2017-02-27 16 views
0

ページにメディアライブラリファイル全体を表示するコードがあります。私は、現在ログインしているユーザーとアップロードしたファイルだけにメディアファイルを表示する機能を実現したいと考えています。 ユーザーがファイルをアップロードしていない場合は、ファイルがアップロードされていないと言ってください。Wordpress:ログインしているユーザーに基づいてページにメディアファイルを表示

現在のPHPのスニペット: - 私は右に最終的にそれを得ることができたコードで遊んで後>https://wordpress.org/plugins/list-media/

+0

既に試したことが表示される場合があります。 – swa66

答えて

0

<?php 
class list_media { 
    public function list_media_controller($atts, $content = "") { 
     //Get the Shortcode attributes 
     extract( 
      shortcode_atts( 
       array( 
       'order' => 'ASC', 
       'posts_per_page' => -1, 
       'post_status' => null, 
       'post_parent' => null, 
       'default_styles' => true, 
       'date_format' => "Y/m/d" 
       ), $atts 
      ) 
     ); 

     //Query ARGS 
     $args = array(
     'post_type' => 'attachment', 
     'author' => $user_ID, 
      'posts_per_page' => $posts_per_page, 
      'numberposts' => $numberposts, 
      'post_type' => 'attachment', 
      'post_status' => $post_status, 
      'post_parent' => $post_parent, 
      'orderby' => $order_by, 
      'order' => $order 
     ); 

     //Print the first part of the Table 
     echo " 
     <table class='list_media'> 
      <thead> 
       <tr> 
        <th class='header'>File</th> 
        <th class='header'>Author</th> 
        <th class='header'>Date</th> 
       </tr> 
      </thead> 
      <tbody> 
     "; 
    if(is_user_logged_in()) { 

    //get the currenly logged in user 
    $user_ID = get_current_user_id(); 
    } 
     //Get the Media files 
     //$attachments = get_posts($args); 
      $attachments = new WP_Query($args); 
     if (!empty($attachments)) { 
      foreach ($attachments as $attachment) { 
       $attachment_id = $attachment->ID; 
       $attachment_title = get_the_title($attachment_id); 
       $attachment_url = wp_get_attachment_url($attachment_id); 
       $attachment_author_id = $attachment->post_author; 
       $attachment_author_url = get_author_posts_url($attachment_id); 
       $attachment_author_name = get_the_author_meta("user_nicename", $attachment_author_id); 
       $attachment_publish_date = get_the_date($date_format, $attachment_id); 

       $attachment_post_parent_id = $attachment->post_parent; 

       if (!empty($attachment_post_parent_id)) { 
        $attachment_post_parent_title = get_the_title($attachment_post_parent_id); 
        $attachment_post_parent_url = get_permalink($attachment_post_parent_id); 
       } else { 
        $attachment_post_parent_url = "#!"; 
        $attachment_post_parent_title = "Unattached"; 
       } 

       echo " 
       <tr> 
        <td class='title'> 
         <a href='$attachment_url' target='_blank'> 
          $attachment_title 
         </a> 
        </td> 
        <td class='author'> 
         <a href='$attachment_author_url' target='_blank'> 
          $attachment_author_name 
         </a> 
        </td> 
        <td class='date'> 
         $attachment_publish_date 
        </td> 
       </tr> 
       "; 
      } 
     } 

     //Print end of the Table 
     echo " 
      </tbody> 
     </table> 
     "; 
    } 
} 
add_shortcode('list_media', array('list_media', 'list_media_controller')); 

?> 

これは、既存のプラグインです。ここに答えを加える。私はそれが他人を助けると確信しています。

class list_media { 
    public function list_media_controller($atts, $content = "") { 
     //Get the Shortcode attributes 
     extract( 
      shortcode_atts( 
       array( 
       'order' => 'ASC', 
       'posts_per_page' => -1, 
       'post_status' => null, 
       'post_parent' => null, 
       'default_styles' => true, 
       'date_format' => "Y/m/d" 
       ), $atts 
      ) 
     ); 

     //Default table styles 
     if ($default_styles == true) { 
      $default_styles = " 
       <style> 
        .list-media-table { 
         display: block; 
         width: 100%; 
         height: auto; 
         border-collapse: collapse; 
        } 

        .list-media-table th, 
        .list-media-table td { 
         border: 1px solid black; 
         vertical-align: top; 
        } 

        .list-media-table .header { 
         font-size: 16px; 
         font-weight: bold; 
         color: #000; 
         padding: 5px 5px; 
        } 

        .list-media-table td { 
         font-size: 14px; 
         padding: 5px 5px; 
         border: 1px solid black; 
        } 

        .list-media-table .attachment img { 
         width: 72px; 
         height: 72px; 
        } 
       </style> 
      "; 
     } 

     //Print the first part of the Table 
     echo " 
     <table class='list_media'> 
      <thead> 
       <tr> 
        <th class='header'>File</th> 
        <th class='header'>Author</th> 
        <th class='header'>Date</th> 
       </tr> 
      </thead> 
      <tbody> 
     "; 
    if(is_user_logged_in()) { 

    //get the currenly logged in user 
    $user_ID = get_current_user_id(); 
     //Query ARGS 
     $args = array(
     'post_type' => 'attachment', 
     'author' => $user_ID, 
      'posts_per_page' => $posts_per_page, 
      'numberposts' => $numberposts, 
      'post_type' => 'attachment', 
      'post_status' => $post_status, 
      'post_parent' => $post_parent, 
      'orderby' => $order_by, 
      'order' => $order 
     ); 
     $attachments = get_posts($args); 

    } 


     //Get the Media files 
     //  $attachments = new WP_Query($args); 


     if (!empty($attachments)) { 
      foreach ($attachments as $attachment) { 
       $attachment_id = $attachment->ID; 
       $attachment_title = get_the_title($attachment_id); 
       $attachment_url = wp_get_attachment_url($attachment_id); 
       $attachment_author_id = $attachment->post_author; 
       $attachment_author_url = get_author_posts_url($attachment_id); 
       $attachment_author_name = get_the_author_meta("user_nicename", $attachment_author_id); 
       $attachment_publish_date = get_the_date($date_format, $attachment_id); 

       $attachment_post_parent_id = $attachment->post_parent; 

       if (!empty($attachment_post_parent_id)) { 
        $attachment_post_parent_title = get_the_title($attachment_post_parent_id); 
        $attachment_post_parent_url = get_permalink($attachment_post_parent_id); 
       } else { 
        $attachment_post_parent_url = "#!"; 
        $attachment_post_parent_title = "Unattached"; 
       } 

       echo " 
       <tr> 
        <td class='title'> 
         <a href='$attachment_url' target='_blank'> 
          $attachment_title 
         </a> 
        </td> 
        <td class='author'> 
         <a href='$attachment_author_url' target='_blank'> 
          $attachment_author_name 
         </a> 
        </td> 
        <td class='date'> 
         $attachment_publish_date 
        </td> 
       </tr> 
       "; 
      } 
     } 

     //Print end of the Table 
     echo " 
      </tbody> 
     </table> 
     "; 
    } 
} 
add_shortcode('list_media', array('list_media', 'list_media_controller')); 

?> 
関連する問題