Wordpress-如何在循环中获得小尺寸的缩略图

  • 本文关键字:略图 循环 Wordpress- wordpress
  • 更新时间 :
  • 英文 :


我正在从scrath创建一个新主题。博客页面是index.php。我还在其他情况下进行了搜索,并尝试了不同的解决方案,但似乎都不起作用。

我有以下循环:

<a href="<?php the_permalink() ?>" title="<?php echo get_the_title(); ?>"><img class="post-image" itemprop="image" src="<?php echo wp_get_attachment_url(get_post_thumbnail_id($post->ID)); ?>">

这很好用,但会产生全尺寸的图像。

如果我将其更改为:

<a href="<?php the_permalink() ?>" title="<?php echo get_the_title(); ?>"><img class="post-image" itemprop="image" src="<?php echo wp_get_attachment_url(get_post_thumbnail_id($post->ID), 'thumbnail'); ?>">

没有什么变化。Still提供全尺寸图像。

我试着按照这个方法来获取循环中的url:

echo 'aaaaa' . get_the_post_thumbnail_url('thumbnail'); 
echo 'aaaaa' . get_the_post_thumbnail_url('thumbnail'); 

但在这两种情况下,它都只显示"aaaaa"。未返回url。

这是完整的循环:

<?php
$mtw_the_excerpt = get_the_excerpt();
$mtw_headline = substr($mtw_the_excerpt, 0, 100) . '...';
while (have_posts()) : the_post(); 
echo 'aaaaa' . get_the_post_thumbnail_url('thumbnail');                 
?>
<article class="blog-post-in-loop" itemscope itemtype="http://schema.org/Article">
<meta itemprop="author" content="<?php echo get_bloginfo(); ?>">
<meta itemprop="headline" content="<?php echo $mtw_headline; ?>">
<link itemprop="mainEntityOfPage" href="<?php the_permalink() ?>" />
<a href="<?php the_permalink() ?>" title="<?php echo get_the_title(); ?>"><img class="post-image" itemprop="image" src="<?php echo wp_get_attachment_url(get_post_thumbnail_id($post->ID), 'thumbnail'); ?>"></a>
<div class="date">
<time class="date_month" datetime="<?php the_time('c') ?>" itemprop="datePublished"><?php echo get_the_date('m'); ?></time>
<time class="date_year"><?php echo get_the_date('Y'); ?></time>
<time datetime="<?php the_modified_date('c') ?>" itemprop="dateModified"></time>
</div>
<div class="title-exceprt-and-read-more">
<h1 class='blogpost_title' itemprop="name"><a href="<?php echo the_permalink(); ?>"><?php echo get_the_title(); ?></a></h1>
<p class='post-excerpt'><?php echo get_the_excerpt(); ?></p>
<div class='read-more'><a href="<?php echo the_permalink(); ?>"><?php _e('Czytaj więcej','mtw-translation'); ?></a></div>
</div>
<div itemprop="publisher" itemscope itemtype="https://schema.org/Organization">
<div itemprop="logo" itemscope itemtype="https://schema.org/ImageObject">
<meta itemprop="url" content="https://www.example.com/wp-content/uploads/2017/05/logo.jpg">
<meta itemprop="width" content="523">
<meta itemprop="height" content="188">
</div>
<meta itemprop="name" content="<?php echo get_the_title(); ?>">
</div>
</article>
<?php
endwhile;
?>

有人能告诉我哪里出了问题,并为我提供解决方案吗?

从头开始编写主题真是太棒了!提示:使用Wordpress的功能时,请务必参考Wordpress开发人员资源。

因此,根据Wordpress文档的函数get_the_post_thumbnail_url接收到两个参数:

get_the_post_thumbnail_url(int|WP_post$post=null,string|array$size='张贴缩略图'(

第一个参数应该是Post ID、Post对象或Null(如果它在循环中(

尝试:

echo get_the_post_thumbnail_url(get_the_ID(), 'thumbnail');

或者简单地说:

echo get_the_post_thumbnail_url();

最新更新