0

I am trying to add an audio file to my custom post type using a custom term meta field as a link, but I can't get it right.

Here is how I would normally display a custom term meta field:

<?php $terms = get_the_terms($post->ID, 'taxonomy-name');
foreach ($terms as $term) {
$term_id = $term->term_id;
echo get_term_meta( $term_id, 'term-meta-field-name', true );
}?>

I'm assuming I have to turn it into a variable, but I'm not sure how. Any ideas?

<?php echo do_shortcode([audio src="link"]); ?>

Thanks!

0

I figured it out!

Using this code I was able to use my custom meta field as the link in the audio shortcode.

First I created a function in functions.php:

function audio_link() {
    $terms = get_the_terms($post->ID, 'taxonomy-name');
    $result = "";
    if (is_array($terms) || is_object($terms)){
    foreach ($terms as $term) {
    $term_id = $term->term_id;
    $result .= get_term_meta( $term_id, 
    'term-meta-field-name', true );
        }
    }
        return $result;
    }

Then I inserted the function into the shortcode like this:

<?php echo do_shortcode('[audio src="'. audio_link() .'"]');?>

Hope this helps anyone else with this problem!

| improve this answer | |

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