2012-04-13 50 views
1

第一篇文章精選圖像我試圖創建一個'奇怪的'查詢檢索從類別的第一篇文章,並呼應精選圖像的URL - 包裹在img html標籤。Wordpress WP查詢檢索和回聲從類別

不要問我爲什麼這樣做。我認爲我的查詢下面應該理論上的工作,我認爲這是壞的語法在我的PHP,因爲它打破了頁面 - 任何人都可以請幫我修復?

<?php 

    $featureThumb  = new WP_Query(array(

    'post_type'   => 'post', 
    'order'    => 'DESC', 
    'orderby'   => 'date', 
    'posts_per_page' => 1, 
    'cat'    => 4 

)); 

if ($featureThumb->has_post_thumbnail($post->ID)) { 

    $retina = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'homepage-thumb-retina'); 

    echo '<img src="' . $retina[0] . '" alt="' . the_title() . '" width="24" height="24" />' ; 

}; 

endwhile; 

unset($featureThumb); 

endif; wp_reset_query(); 

?> 

回答

1

在這裏你去:

<?php 
$featureThumb = new WP_Query(array(
    'post_type'   => 'post', 
    'order'    => 'DESC', 
    'orderby'   => 'date', 
    'posts_per_page' => 1, 
    'cat'    => 4 
)); 

while ($featureThumb->have_posts()) : $featureThumb->the_post(); 
    if (has_post_thumbnail($post->ID)) { 
     $retina = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'homepage-thumb-retina'); 
     echo '<img src="' . $retina[0] . '" alt="' . the_title() . '" width="24" height="24" />' ; 
    }; 
endwhile; 

unset($featureThumb); 

wp_reset_query(); 
?> 
+0

非常感謝你的兄弟!可以看到我去哪裏錯了。沒想到我需要這個時間,但現在有意義先找回帖子。 – Joshc 2012-04-13 10:12:30

0

也許財產以後這樣

 $postsQuery  = new WP_Query(array(

    'post_type'   => 'post', 
    'order'    => 'DESC', 
    'orderby'   => 'date', 
    'posts_per_page' => 1, 
    'cat'    => 4 

)); 

while ($postsQuery->have_posts()) 
{ 
     $postsQuery->the_post(); 
if(has_post_thumbnail($post->ID)) 
{ 

    $retina = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'homepage-thumb-retina'); 

    echo '<img src="' . $retina[0] . '" alt="' . the_title() . '" width="24" height="24" />' ; 

}; 
} 



unset($postsQuery); 

wp_reset_query(); 
+0

嘿夥計感謝您抽出寶貴的時間,這也可能會奏效。乾杯 – Joshc 2012-04-13 10:13:22