繁体   English   中英

WordPress:基于登录用户在页面上显示媒体文件

[英]Wordpress: Display media files on a page based on logged in user

我有一个代码,可在页面上显示整个媒体库文件。 我想要实现仅向当前登录的用户及其上传的文件显示媒体文件的功能。 如果用户没有上传文件,则应该简单地说没有文件上传。

当前的PHP代码段:

<?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') );

?>

这是一个现有的插件-> https://wordpress.org/plugins/list-media/

在玩完代码之后,我终于能够正确地进行操作。 在这里添加答案。 我相信它会帮助别人。

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') );

?>

暂无
暂无

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

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