0

我需要同时获取帖子缩略图的帖子标题、日期和内容(或摘录),同时分别获取 url 和图像 src。我正在解决这个查询:

SELECT * FROM wp_posts WHERE post_type='post' AND post_status='publish' ORDER BY id DESC LIMIT 5

使用上面的查询,如何获取帖子缩略图?我正在通过这种方式检索所有数据:

<?php
        #start a loop that starts $i at 0, and make increase until it's at the number of rows
        for($i=0; $i< $num_rows; $i++) {

        #assign data to variables, $i is the row number, which increases with each run of the loop
        $blog_date = mysql_result($query_result, $i, "post_date");
        $blog_title = mysql_result($query_result, $i, "post_title");
        $blog_excerpt = mysql_result($query_result, $i, "post_excerpt");
        $blog_content = mysql_result($query_result, $i, "post_content");

        #$blog_permalink = mysql_result($query_result, $i, "guid"); //use this line for p=11 format.
        $blog_permalink = $blog_url . mysql_result($query_result, $i, "post_name"); //combine blog url, with permalink title. Use this for title format

        #format date
        $blog_date = strtotime($blog_date);
        $blog_date = strftime("%b %e", $blog_date);
        ?>

        <div class="post">
            <div class="date"><?php echo $blog_date; ?></div>
            <h2><a href="<?php echo $blog_permalink; ?>"><?php echo $blog_title; ?></a></h2>
            <div class="entry">
                <?php echo $blog_excerpt; ?>
            </div>
        </div>

        <?php } #ends loop ?>

谢谢。

4

1 回答 1

0

您可以在之外使用WP_QueryWordPress

<?php
require( 'your_wordpress_root_folder/wp-load.php' );
$args = array(
    'post_status' => 'publish',
    'post_type' => 'post',
    'posts_per_page' => 5
);
$query = new WP_Query( $args );
while ( $query->have_posts() ) : $query->the_post();
?>
    <h1><a href="<?php the_permalink() ?>"><?php the_title() ?></a></h1>
<?php if ( has_post_thumbnail()) : ?>
    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
<?php the_post_thumbnail() ?></a>
<?php
    endif;
endwhile;
于 2013-05-02T20:51:47.323 回答