I have a situation where:

  • I need to create a custom link per image on an image gallery. I did that via ACF. I have created a text field with HTML outputing so that hyperlinks get converted automatically.
  • The user will be populating that field with the link every time a new post is created.
  • I am using a custom post type (post-portfolio).
  • Each post has a cover image that will be a thumbnail when looking at the gallery.
  • Those links will later on be displayed as a link on the thumbnail of the image - not on the image's lightbox or single-post page.
  • The gallery's default behavior is to have two font-awesome icons acting as buttons/links: Lightbox view and single post permalink. I have now added a new font awesome link/button to have that text field acting as the link's target.

The code on the functions.php:

    register_field_group(array (
        'id' => 'acf_sponsorer-lank',
        'title' => 'Sponsor hyperlänk',
        'fields' => array (
            array (
                'key' => 'field_56ea5ece13165',
                'label' => 'Sponsor länk till webbsida',
                'name' => 'sponsor_lank_till_webbsida',
                'type' => 'text',
                'instructions' => 'Skriv här webbsidan till sponsorn. Om det inte finns någon webbsida skriv endast #',
                'required' => 1,
                'default_value' => '',
                'placeholder' => '',
                'prepend' => '',
                'append' => '',
                'formatting' => 'html',
                'maxlength' => '',
        'location' => array (
            array (
                array (
                    'param' => 'post_type',
                    'operator' => '==',
                    'value' => 'post-portfolio',
                    'order_no' => 1,
                    'group_no' => 1,
        'options' => array (
            'position' => 'normal',
            'layout' => 'default',
            'hide_on_screen' => array (
        'menu_order' => 0,

The code on the gallery that shows all thumbnails:

$variable = get_field('sponsor_lank_till_webbsida', $post->post-portfolio);

<div class="acts">
    <a href="'.$url.'" title="'.get_the_title().'" class="icon-search" rel="prettyPhoto[gallery1]"><!----></a>
    <a href="'.the_field('sponsor_lank_till_webbsida', get_option('acf_sponsorer-lank')).'" class=" icon-h-link" target="_blank"><!----></a> <!-- the link where the problem is -->
    <a href="'.get_permalink().'" class=" icon-link"><!----></a>

A note on the last bit of code I wrote:

The <div class="acts"> is inside of PHP tags, so no need to open and close it there.


The custom field is showing up where on the custom post type page when the admin creates a new post, but the link on the gallery page has no target (the value that the user inserts on the post creation page).

I have been reading the documentation and trying so many times and so many ways but I cannot seem to make it work. Can please somebody help me?

  • For your first bullet I'd consider just using the "URL" field type to make sure you get a valid URL. What you're trying to do isn't completely clear to me but this seems wrong: the_field('sponsor_lank_till_webbsida', get_option('acf_sponsorer-lank'));, you can probably just get away with: the_field('sponsor_lank_till_webbsida'); – corygibbons Mar 21 '16 at 3:50
  • I'd also strongly advise not wrapping the entire 'acts' element in PHP tags, it becomes a bit of a mess with all the concatenation. – corygibbons Mar 21 '16 at 3:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.