Php 从特定类别获取链接和特色图像

Php 从特定类别获取链接和特色图像,php,wordpress,slider,Php,Wordpress,Slider,我试图在wordpress中创建一个自定义滑块并添加我自己的链接,但我无法显示帖子的特色图像。这是我现在正在使用的代码 <?php $posts = get_posts('category=20&orderby=rand&numberposts=6'); foreach($posts as $post) { ?> <?php echo '<li>'; ?> <a href="<?php the_permalink() ?>"

我试图在wordpress中创建一个自定义滑块并添加我自己的链接,但我无法显示帖子的特色图像。这是我现在正在使用的代码

<?php $posts = get_posts('category=20&orderby=rand&numberposts=6'); foreach($posts as $post) { ?>

<?php echo '<li>'; ?>
<a href="<?php the_permalink() ?>" target="_parent"><?php the_title();?> 
<?php wp_get_attachment_image( $attachment->ID, 'small' );?>
</a>
<?php echo '</li>'; ?>

这段代码的工作原理是随机显示链接,但它们从不显示文章的特征图像。请有人帮帮忙,让我知道我做错了什么。。。
谢谢

这是因为
$attachment
变量为空。我建议更换:

<?php wp_get_attachment_image( $attachment->ID, 'small' );?>

改为以下内容:

<?php the_post_thumbnail( 'small' );?>