Support » Plugin: Featured Audio » Fixes and improvements for get_the_featured_audio()

  • Hi,

    When I tried to use get_the_featured_audio(), I noticed that the album art is never shown. That’s because the variable $audio_attachment_id is never set in the function.

    I fixed this, and also added the following features:

    • album art size can be set via param (default thumbnail)
    • the outer div.featured-audio is now annotated with CSS classes has-album-art (if it contains album art) and/or has-title (if it has a title) for advanced CSS styling
    • there’s now a wrapper div.title-player-wrapper around the title and player (for CSS styling)

    Can you include this extension in the next version of the plugin?

    Here’s the updated code:

    
    /**
     * Get the featured audio, if it exists.
     *
     * @param $args                   array   Display options.
     * @param $args['id']             int     Post id (optional). Defaults to current post id. 
     * @param $args['album_art']      boolean Whether to display the album art for the featured audio cycle. Default: false.
     * @param $args['album_art_size'] string  Size to use for the album art picture. Default: thumbnail.
     * @param $args['title']          boolean Whether to display the title of the audio attachment. Default: false.
     *
     * @return string The featured audio markup.
     */
     function get_the_featured_audio( $args = array() ) {
    	$args = wp_parse_args( $args, array(
    		'id'        => null,
    		'title'     => false,
    		'album_art' => false,
        'album_art_size' => 'thumbnail'
    	) );
    
    	$audio_url = get_featured_audio_src( $args['id'] );
      $audio_attachment_id = get_featured_audio_attachment_id( $args['id'] );
      
    	$output = '';
      $outer_css_classes = '';
    	if ( $audio_url ) {
    		$thumb_id = '';
        if ( $args['album_art'] ) {
    			$thumb_id = get_post_thumbnail_id( $audio_attachment_id );
          $outer_css_classes .= ' has-album-art'
        }
        if ( $args['title'] ) {
          $outer_css_classes .= ' has-title'
        }
    		$output = '<div class="featured-audio' . $outer_css_classes . '">';
        
    		if ( ! empty( $thumb_id ) ) {
          add_filter( 'wp_get_attachment_image_attributes', 'featured_audio_filter_album_art_image_attrs' );
          $output .= wp_get_attachment_image( $thumb_id, $args['album_art_size'] );
          remove_filter( 'wp_get_attachment_image_attributes', 'featured_audio_filter_album_art_image_attrs' );
    		}
        $output .= '<div class="title-player-wrapper">';
    		if ( $args['title'] ) {
    			$output .= '<h3 class="featured-audio-title" style="clear: none;">' . get_the_title( $audio_attachment_id ) . '</h3>';
    		}
    
    		// Embedded audio player.
    		$output .= wp_audio_shortcode( array( 'src' => $audio_url ) );
    		$output .= '</div></div>';
    	}
    	return $output;
    }
    
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.