WordPress permette di associare una thumbnail ad un post, pagina o post personalizzato. Se è facilissimo estrarre l’intera immagine da codice, grazie alle funzioni the_post_thumbnail() o get_the_post_thumbnail, che restituiscono il completo tag img, risulta meno intuitivo come ottenere il solo link:
1 2 3 4 | // Se $post_id è l'id del post $image_id = get_post_thumbnail_id($post_id); $image = wp_get_attachment_image_src($image_id, 'full'); echo $image[0] |
Da usare, ad esempio in:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | $args = array('category_name' => 'featured'); $posts = get_posts($args); foreach($posts as $post) : ?> <a href="<?php echo get_post_permalink($post->ID) ?>"> <img alt="" title="<?php echo get_the_title($post->ID) ?>" src="<?php $image_id = get_post_thumbnail_id($post->ID); $image = wp_get_attachment_image_src($image_id, 'full'); echo $image[0] ?>" /> </a> <?php endforeach; |
La funzione wp_get_attachment_image_src() restituisce un array con url, width ed height.








4
Non ci sono commenti per questo Post
Lascia un commento