runningantelope123 runningantelope123 - 5 months ago 10
PHP Question

Using PHP shortcode in HTML style="" CSS

I'm not sure if this is possible but I'll ask anyway...

I'm trying to use the PHP shortcode in Advanced Custom fields

<?php the_field('charitynumber'); ?>


for our client to enter a number on the back end between 1-100. This will effect the height of a div showing/hiding an image based on a overall goal number for a charity. I'm thinking of setting it up like

<div style="<?php the_field('charitynumber'); ?>"><img src="/img_here.jpg"></div>


But cannot think of how to do it or an alternative way.

Answer

You need to put the shortcode in a valid style attribute. That means outputting the style name and appending the appropriate units. Also, if the_field() returns a string, you need to echo it.

<div style="height: <?php echo the_field('charitynumber'); ?>%;"><img src="/img_here.jpg"></div>