Archived topic
Element: Give img a post link
9 replies · Started by Tim on April 5, 2020
How do I get the link to a post so that I can put it in the listing's <img>
<figure class="publication-header-media">
<?php
$cover = get_field('cover');
$size = 'full'; // (thumbnail, medium, large, full or custom size)
if( $cover ) {
echo wp_get_attachment_image( $cover, $size );
}
?>
</figure>
Hi there,
i assume the link will be from an ACF field, so you'll need to create a variable to get the url. In this example we'll call it $pub_link
Then you can echo the anchor tags around your image like so:
echo '<a href="' . $pub_link .'">' . wp_get_attachment_image( $cover, $size ) . '</a>';
Hi David
I don't think I've explained well.
wpsp displays the H2 with a link to its post.
I want to do the same with the <figure> or <img> in the listing. wpsp does this for me with the Featured Image, but I'm not using the featured image.
It doesn't make sense to me to do this with an ACF field which would require the user to remember to get the URL and enter it which may lead to user error.
Should I not use WP's inbuilt fields such as get_permalink or similar ?
Or could I get the link the same way wpsp is getting the link to add to the H2 in the listing ?
Thanks
OK - then you can do this to get the link:
$pub_link = esc_url( get_permalink() );
Nice.
Followup question; I understand the quotes are for escaping, but what do the dots (periods) represent? ' . $pub_link .'
It means to concatenate :)
Hi David
But is there anything to concantenate there?
Isn’t it just a single string of text that is escaped then escaped?
PHP methods are many:
https://stackoverflow.com/a/5605970
there are some things concatenate can do other methods cant - such as function calls or methods - in this instance its not required - so just my preference for readability and consistency.
Gotcha
You're welcome