Add custom field value in asset request


I have custom field that returns a value for an icon depending on what the poster chooses in a select.
And right now I have solved it like this:
<img src=''<?php bloginfo('template_url'); ?>/dist/images/<?php echo esc_html($hTeam['value']); ?>-icon.svg'' />

(Nevermind the ‘’ instead of ", the code wouldn’t show otherwise)
It works but not perfect and I would much rather use @asset but I’m not sure how to break out of the asset tag and include the value?

<img src="@asset('images/'<?php echo esc_html($hTeam['value']); ?>'-icon.svg')" alt="">

The above doesn’t seem to work.

Could anyone help me what to do to get this working?

I would recommend looking at where that directive is defined to see what it expects:

And also looking at how directives work internally:

One thing that helped me is that it’s easy to think of directives as functions, but they aren’t really a function in the way you think: They’re instructions for generating PHP.

1 Like

I think I needed to do something similar, had no trouble with this:

<img src="@asset('images/vendor-' . $brand['img'])" alt="{{ $brand['name'] }} logo">

I think you can also do (not sure if this works in Sage 9):
<img src="{{ \Roots\asset('images/vendor-' . $brand['img']) }}" alt="{{ $brand['name'] }} logo">

1 Like

Thank you very much both @johnnytest4real and @alwaysblank, both your answers helped me solve it :slight_smile:

This topic was automatically closed after 42 days. New replies are no longer allowed.