2017-06-11 47 views
0

我無法通過標題&顯示在列表中的Wordpress頁面上顯示圖像附件。我正在使用新的WP_Query來生成列表,其中包含來自特定類別的3個帖子。在循環內部,我試圖獲取每篇文章的第一張圖片,用'get_children'數組檢索它們的圖像ID(帖子沒有精選圖像,所以我不能使用the_post_thumbnail)。使用console.log,我可以看到我至少獲得了列表中第一個項目的圖像url,但不是其他人。即使使用我正在使用的那個,我也無法使用wp_get_attachment_thumb_file()來顯示它。這裏是我到目前爲止的代碼:顯示文章標題和圖像附件縮略圖

<div class="inner-left"> 
<ul class="blog-posts-ul"> 
<?php 
global $wp_query; 
$wp_query = new WP_Query(array('order' => 'DESC', 'posts_per_page' => 3, 'cat' => '21,23,689,741,1589')); 
    if (have_posts()) : while (have_posts()) : the_post(); ?> 
    <li> 
     <?php $attachment = get_children(array('post_parent' => get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' => 'image')); 
      if ($attachment) { 
       $attachment = current($attachment); 
       $image_url = wp_get_attachment_thumb_url($attachment->ID); 
       $attachment_id = attachment_url_to_postid($image_url); 
       echo '<script>console.log("thumb: "' . $image_url . ')</script>'; 
      }; ?> 
     <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><?php the_excerpt(); ?> 
     <?php wp_get_attachment_thumb_file($attachment->ID); ?> 
    </li> 
    <?php endwhile; endif; ?>   
</ul> 
<?php // Reset Query 
wp_reset_query(); ?> 
</div><!-- inner-left --> 

測試場地是http://testsite.humortimes.com/,如果你想看看。向下滾動,它的格式爲3格,低於'最新的幽默時代假新聞標題'部分。目前,格式不是很好,我打算有兩列,只有一個顯示。

任何幫助,將不勝感激。謝謝。

+0

什麼是真正令人發瘋的是,使用以下我得到一個有效的附件ID ... '$ attachment_id = $ attachments-> ID;' ...我得到一個有效的圖像URL與'$ image_url = wp_get_attachment_thumb_url($ attachments-> ID);'如'var_dump($ image_url);' ...所示......但是,以下返回'null'。這是爲什麼? '$ image_thumb = wp_get_attachment_thumb_file($ attachment_id); var_dump($ image_thumb);' – jamminjames

回答

0

停止使用控制檯日誌並開始使用var_dump($ image_url);這將打印您直接在該元素上的任何內容。

我不明白爲什麼你不能使用發佈縮略圖?

+0

我不認爲這是有效的,除非你有一個'精選圖像'選擇,這些帖子沒有。另外,我在這段代碼中嘗試了if語句(如果有一個後續縮略圖,則使用它,否則,執行其餘部分),並且它也不會生成任何內容,因爲沒有特色圖像。不過,我應該把它放回去,只是爲了一般目的。 – jamminjames

0

終於找到的東西,將返回一個圖片附件的縮略圖(一說是不是一個有特色的形象,所以無法使用the_post_thumbnail):

wp_get_attachment_image($attachment_id, 'thumbnail') 

我敢發誓我以前試過一個,但無論如何,它現在正在工作,哈利路亞。

但是,我留下了與每個列表項目<li>使用相同的圖像的問題。 (循環包圍列表並位於周圍的<ul>內部)。

然後我在Function Reference/get children找到了一些關於更新數組的更好的信息,這似乎使它至少嘗試更新每個列表項的縮略圖。但是,我沒有得到超出第一個圖像的圖像。那麼,爲什麼不更新?

完整代碼現在:

<div class="inner-left"> 
<ul class="blog-posts-ul"> 
<?php 
global $ht_query; 
$ht_query = new WP_Query(array('order' => 'DESC', 'posts_per_page' => 3, 'cat' => '21,23,689,741,1589')); 
if (have_posts()) : while ($ht_query->have_posts()) : $ht_query->the_post(); 
if (has_post_thumbnail()) { 
    $image_attach = the_post_thumbnail(); 
    } else { 
    $args = array('post_parent' => $post->ID, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'numberposts' => 1, 'order'=> 'DESC'); 
    $attachments = get_children($args,ARRAY_A); 
    $rekeyed_array = array_values($attachments); 
    $child_image = $rekeyed_array[0]; 
    $image_attach = wp_get_attachment_image($child_image['ID'], 'thumbnail'); 
    }; 
?> 
    <li> 
    <a href="<?php the_permalink(); ?>"><?php echo $image_attach; ?><?php $title = the_title('','',false); if(strlen($title) > 50): echo trim(substr($title, 0, 55)).'...'; else: echo $title; endif; ?></a><?php the_excerpt(); var_dump ($child_image['ID']); ?> 
    </li> 
<?php endwhile; endif; ?>   
</ul> 
<?php // Reset Query 
wp_reset_query(); ?> 
</div> 

在端部的的var_dump表明attachment_id並不每次通過更新,因爲它給出了用於第一後每個連續項「NULL」。

任何幫助將不勝感激。

0

經過很多挫折之後,我發現這個nifty function正是我想要的。它查詢數據庫以查找帖子的第一張圖片,我猜想這是我失蹤的關鍵。它也首先測試一個特色圖像,所以這也被照顧。我把這個函數放在我的子函數文件中,只需要在頁面上調用它。

如果您使用它,請注意您需要添加$size = 'thumbnail';,因爲儘管她的描述表明它會返回縮略圖,但它仍返回全尺寸圖像。儘管這個變量在整個函數中都有使用,所以你只需要給它一個值。

謝謝,Amberweinberg!