1

我正在尝试创建一个“奇怪”查询来从一个类别中检索第一个帖子并回显特色图像 URL - 包含在 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();

?>
4

2 回答 2

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();
?>
于 2012-04-13T10:06:18.543 回答
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();
于 2012-04-13T10:00:04.247 回答