• xii

    (@thehiicompanyca)


    How do I pass and display $query variable should display attachment, audio, or video file types from WP media library? So far, I have the code below trying to display attachments to a dedicated page template, however code below only passes “array array array array array…”

    <?php
    $args = array(
        'post_type' => 'attachment',
        'post_status' => 'inherit',
        'posts_per_page' =>25,
        // 'post_parent' => 210, // Post-> ID;
        // 'numberposts' => null,
    );
    
       // The Query
     $query = new WP_Query( $args );
    
    if ( $query->have_posts() ) :
    while ( $query->have_posts() ) : $query->the_post();
    
        // do stuff
        echo $posts;
        
    endwhile;
    endif;
    
    // Reset Post Data
    wp_reset_postdata();
    
    ?>
Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    It’s unclear what $posts is supposed to be (other than some sort of array). It’s unrelated to your query anyway. You need to use appropriate functions for the current post/attachment in place of echo $posts;. Like the_title(); and echo wp_get_attachment_image( get_the_ID(), 'large');.

    Thread Starter xii

    (@thehiicompanyca)

    Hi bcworkz,

    Read and executed below
    How to get wordpress media library attachments

    How do I get one media file and player, audio in this instance, to only play one instance at a time. So for example; if $audio(one) is playing and $audio(two, three, or etc.) play button is pressed, how to stop/ pause currently played $audio
    and only play current?

                <?php
                $args = array(
                    'post_type' => 'attachment',
                    'post_mime_type' =>'audio',
                    'post_status' => 'inherit',
                    'posts_per_page' =>-1,
                );
                
                   // The Query
                 $query = new WP_Query( $args );
                
                if ( $query->have_posts() ) :
                while ( $query->have_posts() ) : $query->the_post();
                
                    // do stuff
                    if ( $post->post_type == 'attachment'){
                        $attachments = get_posts( array (
                            'post_type' => 'attachment',
                            'post_mime_type' =>'audio',
                            'post_status' => 'inherit',
                            'posts_per_page' =>-1,
                            'post_parent' => $post->ID
                            ) );
                    }
                    
                    if ( $attachments ) {
                        foreach ( $attachments as $attachment ){
                            $audio = wp_get_attachment_url( $attachment->ID, 'full', false );
                            // $attachment_id = attachment_url_to_postid( $audio );
                            $meta = wp_get_attachment_metadata( $audio );
                
                            echo $audio; //here outputs the attachemnts ID's of current post
                        }
                    }
                    ?>
                    <article class="audio element">
                       <div class="audio element title"><?php echo the_title();?></div>
                       <div class="audio element image"><?php echo the_post_thumbnail();?></div>
                       <div class="audio element content"><?php echo the_content();?></div>
                    </article>
                    <?
                endwhile;
                endif;
    
    // Reset Post Data
    wp_reset_postdata();
    
    ?>
    Moderator bcworkz

    (@bcworkz)

    I’m not sure how that would be done. Hopefully someone more familiar with media payers will chime in. My best guess would be to install a JavaScript listener on each player. When someone click/taps the play button, the script would loop through the other players to stop playing. I don’t even know how much control JavaScript would have over players. I’m assuming there’s a way for scripts to stop them playing.

    Thread Starter xii

    (@thehiicompanyca)

    Hi bcworkz,

    Upon further research and queries on other forums, it seems that their is a preference towards javascript (a bit out of my wheel house) to execute something like this.

    I’ve temporarily used a thumbnail as a redirect to actual media file until a PHP functionality appears. Is this a functionality beyond PHP capacities?

    I appreciate your comment and suggestion.

    Moderator bcworkz

    (@bcworkz)

    Yes, JavaScript, not PHP. PHP cannot control anything in a browser. All PHP can do is send out initial content.

    For pages with multiple players, it’s common to have static images of players because media players tend to be slow to load. When clicked, JS could swap in the actual player or redirect to a single player page or media file as you say.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘wp_query not displaying post_type attachment, audio, or video files’ is closed to new replies.