2010-06-22 30 views
5

kullanarak doğru yolu wp_get_attachment_image() kullanarak doğru yolu arıyorum.wp_get_attachment_image() wordpress

Aşağıdaki kod:

<?php 
    $args = array(
     'type' => 'attachment', 
     'category_name' => 'portfolio' 
     ); 
    $attachments = get_posts($args); 
    print_r($attachments); 
?> 

şu sonuca üretir:

Array 
(
    [0] => stdClass Object 
     (
      [ID] => 54 
      [post_author] => 1 
      [post_date] => 2010-06-22 00:32:46 
      [post_date_gmt] => 2010-06-22 00:32:46 
      [post_content] => <a href="http://localhost/wordpress/wp-content/uploads/2010/06/Capture.jpg"><img class="alignnone size-medium wp-image-55" title="Capture" src="http://localhost/wordpress/wp-content/uploads/2010/06/Capture-300x114.jpg" alt="" width="300" height="114" /></a> 
      [post_title] => Our Own Site 
      [post_excerpt] => 
      [post_status] => publish 
      [comment_status] => open 
      [ping_status] => open 
      [post_password] => 
      [post_name] => our-own-site 
      [to_ping] => 
      [pinged] => 
      [post_modified] => 2010-06-22 00:40:22 
      [post_modified_gmt] => 2010-06-22 00:40:22 
      [post_content_filtered] => 
      [post_parent] => 0 
      [guid] => http://localhost/wordpress/?p=54 
      [menu_order] => 0 
      [post_type] => post 
      [post_mime_type] => 
      [comment_count] => 0 
      [filter] => raw 
     ) 
) 

aşağıdaki Ancak hiçbir şey döndürmez.

<?php 
    echo wp_get_attachment_image(54, array('300', '300')); 
?> 

Burada yanlış yapıyorum?

cevap

0

wp_get_attachment_image işlevi yalnızca, wordpress'e yüklenen bir görüntü alır, bu da yazı içeriğinde bir görüntü çıkarmaz.

Örnek görüntünüz için gönderinin içeriğini çıktınız.

gibi: echo $attachments['post_content'];

+0

Bu nedenle, Wordpress'in yüklenen bir görüntüyü belirli bir boyutta almak için dahili bir işlevi yok mu? –

+0

Hayır, verdiğiniz işlev doğru olanıdır, ancak ekin fikrini bir gönderi değil, iletmeniz gerekir. –

+0

Ah tamam teşekkürler. Ama yine de resmi, belirtilen boyuta en yakın boyutta döndürmeli, değil mi? –

4

Aslında kabul cevabı gerçekten sorunun cevabının sanmıyorum. wp_get_attachment_image() için;

Senin sorunun, (tipik $post->ID WP deyişiyle sizin örnekte 54) sonrası id geçen olmamız. codex de görülebileceği gibi, sen eki kimliği kullanmak zannediyorsunuz (aşağıda $attachment_id bakınız):

$image_attr = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'medium'); 
: Başka bir deyişle

wp_get_attachment_image($attachment_id, $size, $icon); 

, böyle bir şey yapmak zorundasın Gördüğünüz gibi

+0

Downvoter - düşünceler? – rinogo

0

wp_get_attachment_image fonksiyonu dört değerleri kabul edebilir:

wp_get_attachment_image (int $attachment_id, string|array $size = 'thumbnail', bool $icon = false, string|array $attr = '') 

Yani ben hep kullanın:

<?php echo wp_get_attachment_image(get_the_ID(), array('700', '600'), "", array("class" => "img-responsive")); ?> 

Not: biz sadece() aktif yazının kimliği geçmek için get_the_ID kullanabilirsiniz. ve burada 700, genişlik ve 600, ek görüntünün yüksekliğidir. Ayrıca sınıfımızı dizisi ("class" => "img-responsive") olarak da geçebiliriz.