Support

Account

Home Forums General Issues show custom field for media (img) in theme

Solving

show custom field for media (img) in theme

  • I’ve added a custom field (“image source”) for images.

    How can I show the field if i used the image absolutely normal in a post? And not with an “attachemend” custom field.

  • To use it in a theme you can just do

    $img = get_field('image_source');
    echo "<img src='" . $img['url'] . "' alt='" . $img['alt'] . "'>";

    You can replace (or add srcset attributtes) by replacing $img['url'] with $img['sizes']['large'], $img['sizes']['small'], etc.

    If you wanted to use it actually in a post (as in within the WP editor), your best bet would likely be to create a shortcode in your theme’s functions.php, which I expect would look something like the following:

    function acf_image_function($atts) {
        $img = get_field($atts[0]);
        echo "<img src='" . $img['url'] . "' alt='" . $img['alt'] . "'>";
    }
    add_shortcode('acf_image_shortcode', 'acf_image_function');

    EDIT: then you’d use the shortcode [acf_image_shortcode image_source]

  • Thank you for your answer but I think your solution does not help with my problem.

    I expressed myself a bit awkwardly…sorry.

    I have added a custom field to the file attachments (images) which can be filled out. (like the image alt attribute, description, title…)

    The pictures are inserted in the text via the normal WYSIWYG editor. If the image has a description, I get it automatically (in the editor). Now I want the text of my custom field to be printed below the description (this should have an span around it or something).

    I suspect I can only solve this via functions.php?

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.