• Resolved Marc Saffar

    (@canayou)


    Hello,
    Thanks for your fantastic plugin.

    I would like to place an image ACF field with automatically the post permalink on it.
    I read the documentation, asked an AI and tried this code :

    add_filter('meta_field_block_get_acf_field', function ($content, $field_name, $post_id) {
        if ($field_name === 'logo_presse') {
            $image_id = get_field($field_name, $post_id);
            if ($image_id) {
                $image_url = wp_get_attachment_image_url($image_id, 'full');
                $permalink = get_permalink($post_id);
                $content = '<a href="' . esc_url($permalink) . '"><img src="' . esc_url($image_url) . '" alt=""></a>';
            }
        }
        return $content;
    }, 20, 3);

    I am sure of the field name, I choosedd “ID” in ACF field image, The image displays all right but no link.

    Any help welcome…
    Cheers

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Phi Phan

    (@mr2p)

    Hello Marc @canayou. There is already a snippet of code for this task in the plugin description. Here it is:

    add_filter( 'meta_field_block_get_acf_field', function ( $block_content, $post_id, $field, $raw_value ) {
      $field_name = $field['name'] ?? '';
      
      if ( 'logo_presse' === $field_name && $block_content !== '' ) {
        $block_content = sprintf('<a href="%1$s">%2$s</a>', get_permalink($post_id), $block_content);
      }
    
      return $block_content;
    }, 10, 4);

    Your code was wrong. The parameters were wrong.

    Phi.

    • This reply was modified 9 months, 4 weeks ago by Phi Phan.
    Thread Starter Marc Saffar

    (@canayou)

    Hello Phi Phan @mr2p,
    Thanks a lot !
    I tried your code. It display the permalink url under the image and between ().

    Finally this one works :

    add_filter( 'meta_field_block_get_acf_field', function ( $block_content, $post_id, $field, $raw_value ) {
        $field_name = $field['name'] ?? '';
        
        if ( 'logo_presse' === $field_name && $block_content !== '' ) {
            $permalink = get_permalink($post_id);
            $block_content = sprintf('<a href="%1$s">%2$s</a>', esc_url($permalink), $block_content);
        }
        
        return $block_content;
    }, 10, 4);

    Of course for other people replace logo_press by your ACF field slug

    Cheers.

    Plugin Author Phi Phan

    (@mr2p)

    You are welcome. I’m glad you got it resolved.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Permalink on ACF image’ is closed to new replies.