我無法通過標題&顯示在列表中的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格,低於'最新的幽默時代假新聞標題'部分。目前,格式不是很好,我打算有兩列,只有一個顯示。
任何幫助,將不勝感激。謝謝。
什麼是真正令人發瘋的是,使用以下我得到一個有效的附件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