Answer the question
In order to leave comments, you need to log in
How to assign a URL to a thumbnail in WordPress?
Attaching a link to the title is as easy as shelling pears: <a href="Ссылка">Сам заголовок</a>
But I have no idea how to link to the picture.
Answer the question
In order to leave comments, you need to log in
Then I learned that the function is responsible for the image get_the_post_thumbnail()
, but where it is located is not written.
Is there a way to make the image inherit the link from the header? (which you specify when adding a thumbnail)
UPD: Fuuuh, it was quite difficult, but I was able to figure it out. I have a Gridsby theme (you can check it out). Now these photos open the link that I want.
In general, how I did it:
1. I found out that the link to the post is stored in a function the_permalink()
(how the link turned out to be there, you will find it yourself)
2. I found out in which files it is called. I have this:
- archive-image.php
- page-full-gallery.php
- page-gallery.php
- page-home.php
3. Great, now we need to replace it with our link.
3.1 Create a custom field (Google will help you with this). Never used it, got it right away. I gave the name to the field url_img
3.2 I inserted the code into the files (fortunately, Google helped)
<?php
$custom_fields = get_post_custom($post_id);
$my_custom_field = $custom_fields['url_img']; //'url_img' - название произвольного поля
foreach ( $my_custom_field as $key => $my_url ); //$my_url - то, что вы ввели в произвольном поле
the_permalink()
with echo $my_url;
the_permalink()
better to do target="_blank"
so that the link opens in a new window. For example,<a href="<?php echo $my_url; ?>" target="_blank">
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question