Wordpress get_posts with image

ipakzgxi  于 2023-06-21  发布在  WordPress
关注(0)|答案(2)|浏览(105)

所以,我想显示标题和摘录符合图像?我该怎么做?

<div id="treia_box" style="height:350px; width:350px;"> 
    <?php
    add_filter('excerpt_length', 'new_excerpt_length');
    global $post;
    $args = array( 'numberposts' => 3, 'category' => 29 );
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) :  setup_postdata($post); ?>
    <h2><a href="<?php the_permalink(); ?>"<?php the_title('<h7>', '<h7>'); ?></a></h2>
    <?php $size = array(75,75);
echo get_the_post_thumbnail($post_id, $size)?>
    <h6><?php the_excerpt(); ?></h6> 
    <?php endforeach; ?>    
</div>
dw1jzc5e

dw1jzc5e1#

get_the_post_thumbnail($post_id, $size)是正确的,但是:
1 -您需要指定$size,例如:“large”,“thumbnail”“my_custom_size”

get_the_post_thumbnail($id, 'thumbnail');     // Thumbnail
get_the_post_thumbnail($id, 'medium');        // Medium resolution
get_the_post_thumbnail($id, 'large');         // Large resolution

get_the_post_thumbnail($id, array(100,100) ); // Other resolutions

2 -只有当用户(您)定义了一个帖子缩略图(创建帖子时的特色图像)时,它才会起作用

e4yzc0pl

e4yzc0pl2#

IMHO,最好的方法是:

$posts = get_posts(
    // ...
);
foreach( $posts as $p ){
    global $post;
    $post = $p;
    setup_postdata($post);
    $post->image = (object)[
        "sizes" => my_get_image_size_links()
    ];
}
wp_reset_postdata();

相关问题