• Resolved xii

    (@thehiicompanyca)


    I am trying to create an archive page that displays either an image, audio, or video attachment from the WP media library. Below is the $query I’ve created and added to my template, however nothing gets displayed on site frontend…

    Ive tried echo, return, printf and combinations of such with no results.
    How do I get the $query to display attachments that were called?

    <?php
    // The Query
    $query = new WP_Query( $args );
    
    $args = array(
        'post_type' => 'attachment',
        'post_status' => 'inherit',
        'posts_per_page' =>25,
        'numberposts' => null,
    );
    
    if ( $query->have_posts() ) :
    while ( $query->have_posts() ) : $query->the_post();
      // Do Stuff
      echo ($query);
    endwhile;
    endif;
    
    // Reset Post Data
    wp_reset_postdata();
    
    ?>
    
Viewing 7 replies - 1 through 7 (of 7 total)
  • //This variable will always be empty
    //$query = new WP_Query( $args );
    $args = array(
        'post_type' => 'attachment',
        'post_status' => 'inherit',
        'posts_per_page' =>25,
        'numberposts' => null,
    );
    //place the query after the args
    $query = new WP_Query($args);
    
    
    Thread Starter xii

    (@thehiicompanyca)

    Hi,
    $query = new WP_Query( $args ); was placed after while condition and before endwhile; and I am getting an error response from site. How does attachment from WP media library get displayed on frontend of site?

    <?php
    
    $args = array(
        'post_type' => 'attachment',
        'post_status' => 'inherit',
        'posts_per_page' =>25,
        // 'post_parent' => 210, // Post-> ID;
        'numberposts' => null,
    );
    
    if ( $query->have_posts() ) :
    while ( $query->have_posts() ) : $query->the_post();
      // Do Stuff
      echo ($query);
    
      // The Query
    $query = new WP_Query( $args );
    
    endwhile;
    endif;
    
    // Reset Post Data
    wp_reset_postdata();
    
    ?>
    Thread Starter xii

    (@thehiicompanyca)

    What method or properties will help populate ‘attachment(s)’ on front end of site?
    Right now, echo is displaying ‘array array array array array….’ on front end of site.

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

    You will have to look through every attachment (if any). Right now, it looks like you are getting the attachments.

    I strongly suggest to read this thread:
    https://stackoverflow.com/questions/60974083/get-wordpress-attachments-ids-separated-by-commas-of-current-post

    Thread Starter xii

    (@thehiicompanyca)

    Hi A2hostingrj,

    The suggested thread worked exceptionally well and is versatile enough for usage needs described.

    How would you go about having WP audio/ visual player execute or play only one media file at a time?

    Currently all audio/ visual files will play (AWESOME), however if one is playing and user clicks on media 3,4,5,6 etc. all files will play simultaneously (Not so awesome).

    Currently all audio/ visual files will play (AWESOME), however if one is playing and user clicks on media 3,4,5,6 etc. all files will play simultaneously (Not so awesome).

    Are you using a video player library?

    Thread Starter xii

    (@thehiicompanyca)

    Hi a2hostingrj,

    Upon further research and your recommended link, I was using a variable to call when in fact I should have been using the_content() within the loop.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to display attachment, audio, or video files?’ is closed to new replies.