Wordpress wp查询从类别中的第一篇文章中检索和回显特色图像

时间:2012-04-13 09:35:41

标签: wordpress

我正在尝试创建一个“奇怪的”查询来从类别中检索第一个帖子并回显特色图片网址 - 包含在img html标签中。

不要问为什么我这样做。我认为我的查询在理论上应该可行,我认为这是我的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();

?>

2 个答案:

答案 0 :(得分:2)

你走了:

<?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();
?>

答案 1 :(得分: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();