• Current functionality: the <a> tag is present, regardless of whether the href attribute contains a value. In my case, 2 of the 4 boxes are links, while the other 2 are more informational, and I don’t want the user to be able to click on them.
    Desired functionality: I would like to output an <a> tag for each of the 4 homepage boxes, but ONLY if the corresponding link fields have a value.

    <a href="<?php echo esc_url(of_get_option('box_link' . $i)); ?>">
    <img src="<?php if(esc_url(of_get_option('box_image' . $i)) != NULL){ echo esc_url(of_get_option('box_image' . $i));} else echo get_template_directory_uri() . '/images/box' .$i. '.png' ?>" alt="<?php echo esc_html(of_get_option('box_head' . $i)); ?>" />
    </a>

    In the code above, I’m thinking a conditional statement wrapped around the opening and closing <a> tag would do the trick. Then, images without a link value would appear in the DOM tree as a standalone <img>, not wrapped in an <a> tag. What would this conditional statement look like?

  • The topic ‘Only output homepage box links if href ISN'T empty’ is closed to new replies.