How do you get the mp3 file extension from an iTunes feed and display it in a WP loop?

I want to pull the 5 most recent podcasts from an iTunes feed and post them along with their audio to a WP page.

The code I have is below
its pulling the feed and displaying the name, details, etc fine but its using the same podcast audio for each item.

                    <div class="podcastfeed">
                    <h4>Recent Podcasts</h4>

                    <?php // Get $feed Feed(s)



                        include_once( ABSPATH . WPINC . '/feed.php' );  
                        $hpFeed=get_post_meta($post->ID, "cmb_hp_feed", true);                          
                        $feed = fetch_feed( $hpFeed );
                        $feed->init();
                        $feed->handle_content_type();
                        foreach ($feed->get_items() as $item)
                        {
                            if ($enclosure = $item->get_enclosure())
                            {
                                 $enclosure->get_link();
                            }
                        }                           
                        if ( ! is_wp_error( $feed ) ) : // Checks that the object is created correctly
                            // Figure out how many total items there are, but limit it to 5.
                            $maxitems = $feed->get_item_quantity( 5 );
                            // Build an array of all the items, starting with element 0 (first element).
                            $feed_items = $feed->get_items( 0, $maxitems );
                        endif;

                                        $attr = array(
                                            'src'      => $enclosure->get_link(),
                                            'loop'     => '',
                                            'autoplay' => '',
                                            'preload' => 'none'
                                            );                          
                    ?> 
                    <ol>
                        <?php if ( $maxitems == 0 ) : ?>
                            <li><?php _e( 'No items', 'my-text-domain' ); ?></li>
                        <?php else : ?>
                            <?php // Loop through each feed item and display each item as a hyperlink. ?>
                            <?php foreach ( $feed_items as $item ) : ?>
                                <li> 
                                    <a href="<?php echo esc_url( $item->get_permalink() ); ?>"
                                        title="<?php printf( __( 'Posted %s', 'my-text-domain' ), $item->get_date('F j, Y') ); ?>">
                                        <p>
                                            <span>
                                                <?php echo esc_html( $item->get_title() ); ?>  
                                            </span>
                                            <span><?php printf( __( '%s', 'my-text-domain' ), $item->get_date('F j, Y') ); ?></span>
                                        </p>
                                    </a>
                                     <?php echo wp_audio_shortcode( $attr );?> 

                                </li>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </ol>
                </div>

Related posts

Leave a Reply

1 comment

  1. I got it working in the end though it took some fiddling with

    The page is now displaying each of the podcasts as audio.

                        <div class="podcastfeed">
    
    
                            <?php // Get $feed Feed(s)
                                include_once( ABSPATH . WPINC . '/feed.php' );
                                 $hpFeed=get_post_meta($post->ID, "cmb_hp_feed", true);
                                // Get a SimplePie feed object from the specified feed source.
                                $rss = fetch_feed( $hpFeed );
                                if ( ! is_wp_error( $rss ) ) : // Checks that the object is created correctly
                                    // Figure out how many total items there are, but limit it to 5.
                                    $maxitems = $rss->get_item_quantity( 5 );
                                    // Build an array of all the items, starting with element 0 (first element).
                                    $rss_items = $rss->get_items( 0, $maxitems );
                                endif;
                            ?> 
                            <?php if ( $maxitems == 0 ) : ?>
                                <ol style="display:none;"><?php _e( 'No items', 'my-text-domain' ); ?></ol>                     
    
                            <?php else : ?>
                                <h4>Recent Podcasts</h4>
                                <ol>
                                <?php // Loop through each feed item and display each item as a hyperlink. ?>
                                <?php foreach ( $rss_items as $item ) : ?>
    
                                    <li> 
                                        <a href="<?php echo esc_url( $item->get_permalink() ); ?>"
                                            title="<?php printf( __( 'Posted %s', 'my-text-domain' ), $item->get_date('F j, Y') ); ?>">
                                            <p>
                                                <span>
                                                    <?php echo esc_html( $item->get_title() ); ?>  
                                                </span>
                                                <span>
                                                    <?php printf( __( '%s', 'my-text-domain' ), $item->get_date('F j, Y') ); ?>
                                                </span>
                                            </p>
                                        </a>
                                        <?php 
                                                if ($enclosure = $item->get_enclosure()){
                                                      $enclosure->get_link();
                                                }
    
                                        ?>                                      
                                        <?php 
                                                $attr = array(
                                                'src'      => $enclosure->get_link(),
                                                'loop'     => '',
                                                'autoplay' => '',
                                                'preload' => 'none'
                                                );  
                                        ?>
    
                                        <?php echo wp_audio_shortcode( $attr );?> 
                                    </li>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </ol>
                    </div>