简体   繁体   English

如何在Wordpress中发表的最新帖子中返回精选图片?

[英]How do I return the featured image on the most recent post made in Wordpress?

I want to add a "Pic of the Week" feature on my homepage. 我想在主页上添加“本周精选”功能。 I set it up so I created a post category "Pic of the Week" and I will make a post and add the pic as the featured image. 我进行了设置,因此创建了一个帖子类别“本周的图片”,我将发布一个帖子,并将图片添加为特色图片。 I'm looking for the PHP to return the last featured image on the last post that was made in a specific category. 我正在寻找PHP返回特定类别的最新帖子中的最新特色图片。

I'm really not sure where to start for this so I apologize for not putting what I've tried. 我真的不知道从哪里开始,所以对我没有尝试过的内容深表歉意。 So far I've found 到目前为止,我已经发现

<?php the_post_thumbnail(); ?>

You may try this (get the last post by category name ad then get the featured image using it's id ) 您可以尝试这样做(获取按category name广告的最新帖子,然后使用其id获取特色图片)

$args = array(
    'category_name' => 'Pic of the Week',
    'posts_per_page' => 1,
    'order_by' => 'date',
    'order' => 'desc'
);

$post = get_posts( $args );
if($post) {
    $post_id = $post[0]->ID;
    if(has_post_thumbnail($post_id)){
        // use one of these
        echo get_the_post_thumbnail($page->ID, 'thumbnail');
        echo get_the_post_thumbnail( $post_id, array(80, 80), array('class' => 'post_thumbnail') );
    }
}

First of all I would get the postID using the get recent posts function : 首先,我将使用获取最新帖子功能获取postID:

wp_get_recent_posts( $args, $output )

This will allow you to get the post ID. 这样您就可以获取帖子ID。 You can then use the follow to get the image. 然后,您可以按照以下步骤获取图像。

$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM