从wordpress帖子获取图像

时间:2016-01-27 16:10:46

标签: wordpress image post

我有我最近4个帖子的标题,但我也需要图片贴。我是一个可怕的程序员

JobHostConfiguration.StorageConnectionString
我试图做这样的事情:

Recent Post

4 个答案:

答案 0 :(得分:1)

我认为您正在寻找get_the_post_thumbnail这是Codex

例如:

get_the_post_thumbnail( $postID,'medium', array( 'class' => 'aligncenter' ));

您可以尝试使用此类产品作为最终产品(未经测试):

// Display them as list

$output = '<ul>';

foreach($recent_posts as $post) {

$link = get_permalink($post['ID']);
$image = get_the_post_thumbnail( $postID,'medium', array( 'class' => 'aligncenter' ));
$title = $post['post_title'];

$output .= '<li>
                <a href="'.$link.'">'.
                    $image
                    .'<h2>'.$title.'</h2>
                </a>
            </li>';

}
$output .= '</ul>';
echo $output;

答案 1 :(得分:1)

它必须有效!

<?php

$recent_posts = wp_get_recent_posts(array(
                    'numberposts' => 4,
                    'category' => 0,
                    'orderby' => 'post_date',
                    'post_type' => 'post',
                    'post_status' => 'publish',
));

foreach($recent_posts as $single_post){
    $get_post_images = get_attached_media( 'image', $single_post['ID'] );
    $get_post_images = array_shift( $get_post_images );
    $first_image_url = $get_post_images->guid;


?>

<a href="<?php echo get_permalink($single_post['ID']) ?>"><?php echo $single_post['post_title'] ?></a><br />  
<img src="<?php echo $first_image_url; ?>" /><br />

<?php    
}

?>

答案 2 :(得分:0)

如果您没有帖子缩略图,但是帖子中有图片,那么您需要在循环内使用此解决方案:

$get_post_images = get_attached_media( 'image', $post->ID );
$get_post_images = array_shift( $get_post_images );

//Get url for image
$first_image_url = $get_post_images->guid;

// Show the image
echo '<img src="'. $first_image_url .'" />';

答案 3 :(得分:0)

我明白了!

<ul>
<?php
include('esenergy/wp-load.php'); // Blog path
function recentPosts() {
    $rPosts = new WP_Query();
    $rPosts->query('showposts=3');
        while ($rPosts->have_posts()) : $rPosts->the_post(); ?>
            <li>
                <a href="<?php the_permalink();?>"><?php the_post_thumbnail('recent-thumbnails'); ?></a>
                <h2><a href="<?php the_permalink(); ?>"><?php the_title();?></a></h2>
            </li>   
        <?php endwhile; 
    wp_reset_query();
}
?>
</ul>
<?php echo recentPosts(); ?>