1

I am trying to call a custom term meta field in an audio shortcode that I have placed on a custom page that I am working on. The field contains a link to a .mp3 file that I have uploaded to my media library. I have an IF statement that will only show the audio player along with the text "Audio", if the field actually contains a link to an audio file. However, Nothing is showing up on the front end, even though there is a link in the field.

I did a var_dump on the field, and it outputted:

string(95) "echo do_shortcode([audio src="http://www.foo.com/wp-content/uploads/2018/07/audio.mp3"])"

Here is the code that I currently have in my functions.php:

function first_audio() {
                        $terms = get_the_terms($post->ID, 'firstb');
                        $result = "";
                        if (is_array($terms) || is_object($terms)){
                        foreach ($terms as $term) {
                            $term_id = $term->term_id;
                            $result .= 'echo do_shortcode([audio src="'.  get_term_meta($term_id, 'audioreco_64856', true)  .'"])';

                        }
                    }
                    if (!empty(get_term_meta($term_id, 'audioreco_64856', true))) {
                        return $result;}
                    }

And here is the code that I have on my page:

$first_audio = first_audio();
if (!empty($first_audio)) { ?>
<p><?php first_audio(); ?></p>
<?php }
    ?>

I also tried echoing first_audio(); but that just echoed the whole link, just like the var_dump did.

1

Replace following line

 $result .= 'echo do_shortcode([audio src="'.  get_term_meta($term_id, 'audioreco_64856', true)  .'"])';

with

$result .=  do_shortcode('[audio src="'.  get_term_meta($term_id, 'audioreco_64856', true)  .'"]');

Echo the result of function

<p><?php echo first_audio(); ?></p>
  • Worked like a charm. Thanks! I tried that earlier, but I guess I messed up with the quotation marks... – Darren Sep 17 '18 at 17:00
  • One more question, If I would like to put something like this in front of do_shortcode , how would I write it? <p>get_term_meta($term_id, 'short_27939', true)</p><br> – Darren Sep 17 '18 at 17:14
  • add $result .= "<p>".get_term_meta($term_id, 'short_27939', true)."</p>"; above shortcode – Aparna_29 Sep 18 '18 at 10:59
  • Thanks! I noticed that if there are 2 taxonomies saved to the post, but only one of them has a link to an audio file, the audio player doesn't show up. Only id=f they both have audio files does it show up. Any idea why? – Darren Sep 18 '18 at 18:00

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.