0

我有一个奇怪的问题,我希望这只是因为我错过了一些明显的东西......

我们的 Wordpress 安装最近升级到了 3.8.1。我们在网站目录中使用 WordPress 作为我们新闻项目的一部分。我们循环浏览网站其他部分的帖子以显示最新消息等

我们启用了特色图片,并使用以下代码在 wordpress/news 主页上显示特色图片:

<a href="<?php the_permalink() ?>"><div class="snippet-featured-img"><?php the_post_thumbnail('thumbnail'); ?></div></a>

这工作正常,并输出 150 像素 x 150 像素的特色图像。

我们还希望在我们在网站其他页面上循环浏览的帖子旁边显示特色图片,但我们似乎无法让它发挥作用。

这是代码:

foreach($posts as $post) {
setup_postdata($post);
if ( has_post_thumbnail() ) {
echo "<a href='";
echo the_permalink();
echo "'>";
the_post_thumbnail(array('50','50'));
echo "</a>";
}
echo "<h2><a href=\"";
echo the_permalink();
echo "\" rel=\"bookmark\" title=\"Permanent Link to ";
echo the_title();
echo "\">";
echo the_title();
echo "</a></h2>\n<p>";
$string = strip_tags(strip_shortcodes($post->post_content));
echo chop_string($string,190,'...');
echo " <a href=\"";
echo the_permalink();
echo "\" rel=\"bookmark\" title=\"Permanent Link to ";
echo the_title();
echo "\">";
echo "Read More</a></p>\n";
echo "<div class='clear'></div>\n";
} 

post var 在脚本的上方声明了一点:

$posts = get_posts('numberposts=8&orderby=date');

除了图像之外的所有内容都被输出,甚至图像应该输出的链接也被输出,这表明 has_post_thumbnail() 函数已返回 true。正如我所说,它也在 wordpress 页面本身上工作。

我尝试过传递“缩略图”而不是大小数组,以及 get_the_post_thumbnail(),但我不知道为什么它不起作用!

任何帮助都会很棒

谢谢

4

2 回答 2

0

使用这个使用 get 函数。

<?php echo get_the_post_thumbnail($page->ID, 'thumbnail'); ?>


    get_the_post_thumbnail($post_id);                  // without parameter -> Thumbnail

    get_the_post_thumbnail($post_id, 'thumbnail');     // Thumbnail
    get_the_post_thumbnail($post_id, 'medium');        // Medium resolution
    get_the_post_thumbnail($post_id, 'large');         // Large resolution
    get_the_post_thumbnail($post_id, 'full');          // Original resolution

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

在您的自定义帖子类型中,您是否在自定义循环中设置了 postdata( $post )?如果没有,has_post_thumbnail() 可能没有定义/可用?

EDIT:

尝试添加:

setup_postdata( $post );

就在之前:

$loop->the_post();

And then see if has_post_thumbnail() returns true?

Or, try passing the $post->ID to your call to has_post_thumbnail()?

has_post_thumbnail( $post->ID );
于 2014-02-14T09:36:33.543 回答
0

尝试改变

the_post_thumbnail(array('50','50'));

echo get_the_thumbnail($post->ID, array(50,50));
于 2014-02-14T09:38:35.047 回答