0

I am trying to find out how to create a playlist on the category page. The playlist will play all the songs/audio files associated with posts linked to a given category.

Example If i have a category called "gospel", and 10 posts assigned to "gospel". Each post has a song associated with it. Hence , 10 total songs.

Goal is to have a playlist such that when i go to the category page for "gospel", i see a playlist that can play those 10 songs. As i add new posts and assign it to the category, it should also automatically sync and play it.

i appreciate any assistance

1
  1. get a 10 posts from category with slug "gospel"
  2. get post children in loop, filter by mime type to get proper files
  3. add to list

    /**
     * get 10 post from category with SLUG gospel
     */
    $args = array(
        'category_name' => 'gospel',
        'posts_per_page' => 10
    );
    $songs = array();
    $the_query = new WP_Query( $args );
    if ( $the_query->have_posts() ) {
        while ( $the_query->have_posts() ) {
            $the_query->the_post();
            /**
             * get post children, filter by post_mime_type, limit 1
             */
            $args = array(
                'post_parent' => get_the_ID(),
                'post_type'   => 'attachment', 
                'posts_per_page' => 1,
                'post_status' => 'any',
                'post_mime_type' => 'audio'
            );
            $attachments = get_children( $args );
            if ( $attachments ) {
                foreach ( $attachments as $attachment ) {
                    $songs[] = wp_get_attachment_url( $attachment->ID);
    
                }
            }
        }
    }
    /**
     * print songs
     */
    print_r($songs);
    /**
     * reset postdata
     */
    wp_reset_postdata();
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.