[Resolved] Element: Give img a post link

Home Forums Support [Resolved] Element: Give img a post link

Home Forums Support Element: Give img a post link

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #1227114
    Tim

    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> 
    #1227268
    David
    Staff
    Customer Support

    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>';

    #1229293
    Tim

    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

    #1229664
    David
    Staff
    Customer Support

    OK – then you can do this to get the link:

    $pub_link = esc_url( get_permalink() );

    #1230593
    Tim

    Nice.

    Followup question; I understand the quotes are for escaping, but what do the dots (periods) represent? ' . $pub_link .'

    #1230851
    David
    Staff
    Customer Support

    It means to concatenate 🙂

    #1232060
    Tim

    Hi David

    But is there anything to concantenate there?

    Isn’t it just a single string of text that is escaped then escaped?

    #1232385
    David
    Staff
    Customer Support

    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.

    #1233417
    Tim

    Gotcha

    #1233576
    David
    Staff
    Customer Support

    You’re welcome

Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.