Skip Navigation

[Resolved] how to show caption and description in audio custom fields

This support ticket is created 3 years, 9 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 3 replies, has 2 voices.

Last updated by Nigel 3 years, 9 months ago.

Assisted by: Nigel.

Author
Posts
#2269749
audio.PNG

Hi, I need to show caption and description of the loaded audio through a toolset custom audio field

#2270275

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Andrea

You'll need to register a custom shortcode to be able to output the caption and description, and then use that shortcode where you want them to appear.

You can add the following to Toolset > Settings > Custom Code to register the shortcode 'media-field':

/**
 * Register shortcode 'media-field'
 * 
 * Pass URL of media as shortcode content
 * Attribute 'output' specifies what to return, defaults to media title
 * 
 * example usage:
 * [media-field output='alt']...url here...[/media-field]
 */
add_shortcode('media-field', function ($atts = [], $content = null) {

	// provide default
	$atts = shortcode_atts(
		array(
			'output' => 'title',
		),
		$atts
	);

	$output = '';

    if (isset($content)) {
        $url = do_shortcode( $content );

        // get the ID of the media post for the URL contained in content
        $media_id = attachment_url_to_postid($url);

        $media = get_post($media_id);

        switch ($atts['output']) {
            case 'title':
                $output = $media->post_title;
                break;
            case 'caption':
                $output = $media->post_excerpt;
                break;
            case 'description':
                $output = $media->post_content;
                break;
            case 'alt':
                $output = get_post_meta( $media_id, '_wp_attachment_image_alt', true );
                break;
        }
	}
	return $output;
});

You need to provide the audio field URL as the shortcode content, e.g.

[media-field output="caption"][types field='mp3-file' output='raw'][/types][/media-field]
#2270631

Hi Nigel, thanks for the quick reply but the custom field is audio type and not link type and nothing is displayed

#2271137

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

I just checked on my own test site and it works.

With an audio custom field, when you add an audio file (e.g. an MP3 file) it is added to the WordPress Media Library, and an attachment post for the file is created which stores the data for description, caption etc. These can be edited within the Media Library.

The audio custom field itself stores the url of the mp3 file.

If you have registered the custom shortcode and it is active then you only need to make sure you are outputting the url of the file within the opening and closing media-field shortcodes. That's what the types shortcode does. Make sure that you edit my example to use your own custom field slug ("mp3-file" in my example).

You can check that the types shortcode is outputting the url as expected by adding just the types shortcode to your template, without the surrounding media-field shortcode.