• Resolved ferdman

    (@ferdman)


    Hi, great plugin, I already gave it 5 stars!

    I am using it within the query loop block to display images whereas the image field was created via Advanced Custom fields. Now what I want is to wrap the image with a link to the post within this loop. I know that the default “Featured Imaged” block has the option “Link to post”. How could I achieve that? Is there a code snipped you could provide me with?

    Thank you!

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

    (@mr2p)

    Thank you @ferdman for your rating and your kind words. I’ll create a snippet code for this use case very soon.

    Thanks, Phi.

    Plugin Author Phi Phan

    (@mr2p)

    Hi @ferdman, below is the snippet code for your request, please replace your_image_field_name with your real fieldname.

    add_filter(
        'meta_field_block_get_block_content', function ( $content, $attributes, $block, $post_id ) {
            $field_name = $attributes['fieldName'] ?? '';
            if ('your_image_field_name' === $field_name) {
                return sprintf('<a href="%1$s">%2$s</a>', get_permalink($post_id), $content);
            }
    
            return $content;
        }, 10, 4
    );

    In the future version, I’ll consider adding a ‘Link to post’ setting for some field types like images, text, etc.

    Please feel free to contact me if you need help with this block in future.

    Phi.

    Thread Starter ferdman

    (@ferdman)

    Worked perfectly, thanks a lot for the quick reply!

    So glad I found this topic!

    The filter works perfectly, thank you @mr2p !

    And thank you @ferdman for creating this topic

    Plugin Author Phi Phan

    (@mr2p)

    Hi @charlie67p, I’m very glad you found it useful. This code snippet has already been added to the plugin description. There are also a bunch of others, please check them out in the plugin description, you may need some of them in the future.

    Thanks, Phi.

    Thanks @mr2p !

    I did not see this useful list of snippets! Well done!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Link meta field to post in query loop’ is closed to new replies.