Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
WPB Cup
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

Så här länkar du automatiskt featured images till utvalda inlägg i WordPress

Nyligen frågade en av våra läsare om det var möjligt att automatiskt länka featured images till blogginlägg i WordPress.

De flesta WordPress-teman länkar featured images till inlägg som standard, men vissa teman kanske inte gör det.

I den här artikeln visar vi dig hur du automatiskt länkar featured images till inlägg i WordPress.

Automatically link featured images to posts in WordPress

Varför Link Featured Images till inlägg i WordPress?

Eftersom bilder är mer engagerande än text kan användning av featured images bidra till att öka engagemanget hos användarna på din WordPress blogg.

Vanligtvis är featured images stora och tar upp mer utrymme än text. De är mer färgstarka och därmed mer notice. De är också lättare att clicka på mindre devices som gillar mobiltelefoner och surfplattor.

List25 using featured images to engage users

Men om dina inläggsminiatyrer inte är klickbara gör det det svårare för användare att se ditt inlägg.

De flesta teman i WordPress länkar featured images till inläggen som standard.

Men vissa themes kanske inte använder den metoden, vilket gör det svårt för you att använda featured images på rätt sätt.

Med detta sagt, låt oss se hur du automatiskt kan länka featured images till utvalda inlägg i WordPress.

Automatiskt Link Featured Images till inlägg i WordPress

Denna metod är obligatorisk för att du ska kunna add to kod i dina WordPress-filer. Om du inte har gjort det tidigare kan du läsa vår guide för nybörjare om hur du klistrar in snippor från webben i WordPress.

You can add this code to your theme’s functions.php file. Men ett bättre sätt att lägga till custom code i WordPress är att använda ett code snippets plugin.

function wpb_autolink_featured_images( $html, $post_id, $post_image_id ) {
$html = '<a href="' . get_permalink( $post_id ) . '" title="' . esc_attr( get_the_title( $post_id ) ) . '">' . $html . '</a>';
return $html;
}
add_filter( 'post_thumbnail_html', 'wpb_autolink_featured_images', 10, 3 );

Den här koden addar helt enkelt en länk runt den kod som genereras för att visa featured images eller inläggsminiatyrer på din webbplats.

Denna kod kommer också att lägga till en länk runt featured images på sidor med enskilda inlägg. Om du inte vill länka featured images på ett enskilt inlägg till samma inlägg, använd då denna kod.

function wpb_autolink_featured_images( $html, $post_id, $post_image_id ) {

If (! is_singular()) { 

$html = '<a href="' . get_permalink( $post_id ) . '" title="' . esc_attr( get_the_title( $post_id ) ) . '">' . $html . '</a>';
return $html;

} else { 

return $html;

}

}
add_filter( 'post_thumbnail_html', 'wpb_autolink_featured_images', 10, 3 );

Vi rekommenderar att du använder WPCode för att add to custom code i WordPress.

WPCode WordPress code snippets plugin

WPCode är det bästa code snippets pluginet och det används av över 1 miljon websites. Det gör det enkelt att add code snippets i WordPress utan att behöva editera functions.php-filen i ditt theme.

Först måste du installera och aktivera det gratis pluginet WPCode. Om du behöver hjälp kan du följa vår tutorial om hur du installerar ett plugin för WordPress för steg-för-steg-instruktioner.

När pluginet har aktiverats kommer det att finnas ett nytt menu item med namnet ”Code Snippets” i din WordPress admin bar.

Om du klickar på den visas en lista över de code snippets som du har saved på din site. Eftersom du precis har installerat tillägget kommer din lista att vara tom.

För att lägga till ditt första code snippet i WordPress klickar du på knappen ”Add New”.

Click the Add New Button to Add Your First Custom Code Snippet in WPCode

Därefter kommer du att se Add Snippet page.

Gå till alternativet ”Add Your Custom Code (New Snippet)” och klicka på knappen ”Use snippet”.

Add your new custom code snippet in WPCode

Efter det måste du ge ditt code snippet ett namn. Det kan vara vad som helst som hjälper dig att komma ihåg syftet med koden.

Sedan klistrar du in det snippet som du kopierade ovan i boxen ”Code Preview”. Glöm inte att välja ”PHP Snippet” som code type från dropdown-listan till höger.

Paste code into WPCode to automatically link featured images to posts

Därefter rullar du ner på sidan till section Infogaren.

You can leave it on the ’Auto Insert’ method, so it will automatically insert and execute the code snippet in the proper place.

Pick the insertion method for your custom code snippet

Slutligen ska du toggla från ”Inaktiverad” till ”Aktiv”.

Klicka sedan på knappen ”Save Snippet”.

Save and activate your custom code snippet

Nu är det klart. Nu kommer dina featured images automatiskt att länkas till dina posts.

Vi hoppas att den här artikeln hjälpte dig att lära dig hur du automatiskt länkar featured images till inlägg i WordPress. Du kanske också vill se vår guide om hur du lägger till captions till featured images i WordPress eller vår lista över de bästa programvarorna för webbdesign.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

Avslöjande: Vårt innehåll stöds av våra läsare. Det innebär att om du klickar på några av våra länkar, kan vi tjäna en provision. Se hur WPBeginner finansieras, varför det är viktigt, och hur du kan stödja oss. Här är vår editoriala process.

Avatar

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

Den ultimata WordPress-verktygslådan

Få GRATIS tillgång till vår verktygslåda - en samling WordPress-relaterade produkter och resurser som varje professionell användare bör ha!

Reader Interactions

28 kommentarerLämna ett svar

  1. Syed Balkhi

    Hey WPBeginner readers,
    Did you know you can win exciting prizes by commenting on WPBeginner?
    Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
    You can get more details about the contest from here.
    Start sharing your thoughts below to stand a chance to win!

  2. Fredrick John

    Worked like charm, thank you.

    • WPBeginner Support

      You’re welcome!

      Administratör

  3. Nelson Nchopereu

    How do I put my featured image such that if the post link on WhatsApp or Facebook is shared it goes with the featured image?
    Thanks

  4. Anas

    Great Sir, Its works….

    • WPBeginner Support

      Thank you, glad our content was helpful :)

      Administratör

  5. Rohit Sharma

    How does wpbeginner design its featured images and YouTube thumbnails?
    What tools / apps / software do you use?

    They look pretty awesome..

    I couldn’t find this on your website blueprint post..so I thought of asking here..

    Waiting for your reply..!

    • WPBeginner Support

      Unless I hear otherwise, we use Camtasia for our videos and Photoshop or Affilinty designer for the article thumbnails :)

      Administratör

  6. özkan

    Hello,

    Im so jealous of all these people that says it works.

    It doesnt work for my site. Can anybody help me about this ?

    Thank you.

    • WPBeginner Support

      You may want to reach out to your theme’s support to see if there is a theme specific setting that could be overriding this.

      Administratör

  7. Faizan

    It Worked! Thanks

  8. Carlos Alberto Romay

    Hi there! Awesome article, I am need to do the opposite; to discover HOW TO avoid any link from image, title or text to any url, how can I do this? I mean, you reah my website, see the blog but you CAN´t click any image, text or title, is this possible? thanks

  9. Jennifer

    Hi, Thank you very much.

    I have used it in my website.

    How to add link to some thumbnails in specific size?

  10. pete

    works for me thanks!!

  11. Stuart Campbell

    How can I use this method to have the featured image link to another url? What am I missing here? Thanks

  12. Sandeep

    On my blog the featured images are not clickable.so it becomes a little difficult for the user to navigate to blogs.

    I think the theme whic i am using that doesn’t support that it.

    But the code you have mentioned above i will try it .
    i will try it , if it worked , that will great.

  13. Susan Howarth

    Thank you so much for this!! I was worried this issue would take a lot of time and energy to figure out. After a quick copy and paste of your code, and it worked perfectly! :)

  14. Deepak

    it worked, i wish to know how to close the php statement ? when i use this at the end } my site gives 500 error

  15. Ivan

    It worked fine! Great! Thank you

  16. modo seyoum

    i’m looking for the best way to add links to a featured image. for instance– if you click on the link below, you’ll see the header image and then two links in the content region. i’d rather have those two links appear in the header in a way that doesn’t compromise any responsive design. i have a few ideas, but i’d like to hear from a different perspective…

  17. jeet sandhu

    Hie, i have a different query. I tried displaying specific posts in specific pages using ’post in page’ plugin. But the featured images that i have set for posts doesn’t show on the page. I mean only the title of the post is showing, but I want the featured images of all the posts to be displayed, too! Can you suggest me any way or plugin that can help me achieve this. I have searched the entire web; no one has the answer. I would be grateful if you could help me.

  18. malik aadil

    I am new blogger this article helped me a lot

    I was using links of in the images but the problem was Images are opening but not going to linked article

    • WPBeginner Support

      Hi Malik,

      We are glad you found this article helpful :) Don’t forget to follow us on Facebook for more WordPress tips and tutorials.

      Administratör

  19. Sulman Qamar

    hey dear ,
    here isn’t concerning solely featured pictures however additionally concerning committal to writing . …
    in some model that’s operating.. most of your data is nice on behalf of me and my members ..
    we follow principally .:)
    in this richest article I actually have found totally different sort of data ..
    Hope you best for your blogging future :)
    ~salman qamar

  20. Dale Reardon

    Hi,

    Thanks very much for this useful code.

    Is this a way (can I trouble you for the function code) to also add an ALT tag to the image link with the title of the post being linked to as the alt text?

    I’m vision impaired and at the moment the link just says the image filename which isn’t useful much of the time.

    Thanks,
    Dale.

  21. Ankit Agarwal

    Thanks for his tutorial. I tried this on my blog but realized that the image being shown in the thumbnail is not my featured image but the first image I am using in the post. Probably because I am using MotoPress page builder plugin. and

    There seems to be a conflict between this code and the Motopress plugin.

  22. Dale Reardon - My Disability Matters

    Hi,

    That worked fantastically.

    Only one further query – my website is all about disability issues:

    so I want things to be accessible and I’m blind myself so appreciate all sites doing their bit.

    Is there a way to assign ALT text to this image link composed of the title of the page it is linking to? At the moment my screen reader just reads out the picture file name which sometimes isn’t very helpful.

    Your help most appreciated.

    Dale.

Lämna ett svar

Tack för att du väljer att lämna en kommentar. Tänk på att alla kommentarer modereras enligt våra policy för kommentarer, och din e-postadress kommer INTE att publiceras. Vänligen använd INTE nyckelord i namnfältet. Låt oss ha en personlig och meningsfull konversation.