最佳答案 您可以使用-6ren">
gpt4 book ai didi

WordPress 最近的帖子显示缩略图

转载 作者:行者123 更新时间:2023-12-02 23:31:51 25 4
gpt4 key购买 nike

如何在我最近的帖子中的每个帖子中显示图像或特色图像的小缩略图自定义小部件

这是我的代码:

<?php

include($_SERVER['DOCUMENT_ROOT'] . $root . 'blog/wp-load.php');

$recent_posts = wp_get_recent_posts(array(
'numberposts' => 3
));

?>

<h3 class="divider">Recent Blog Posts</h3>
<ul class="listing">
<?php foreach($recent_posts as $post) { ?>
<li><a href="<?php echo get_permalink($post['ID']) ?>">
<div><?php echo $post['post_title'] ?></div></a></li>
<?php } ?>
</ul>

最佳答案

您可以使用get_the_post_thumbnail

用法:

<?php echo get_the_post_thumbnail( $post_id, $size, $attr ); ?>

与您的代码集成:

<?php foreach($recent_posts as $post) : ?>
<li>
<a href="<?php echo get_permalink($post['ID']) ?>">
<?php echo get_the_post_thumbnail($post['ID'], 'thumbnail'); ?>
<div><?php echo $post['post_title'] ?></div>
</a>
</li>
<?php endforeach; ?>

注意:要启用帖子缩略图,当前主题必须包含 add_theme_support( 'post-thumbnails' );在其functions.php 文件中。另请参阅Post Thumbnails .

来源

Function Reference/get the post thumbnail

另请参阅

Post Thumbnails
Function Reference/add theme support

关于WordPress 最近的帖子显示缩略图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16672246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com