首页 > 解决方案 > Wordpress - 如何在循环内获取小尺寸缩略图

问题描述

我正在从头开始创建一个新主题。博客页面是 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'); ?>">

没有什么变化。仍然给出全尺寸图像。

我试图按照这个以这种方式获取循环内的url:

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

但在这两种情况下,它只呈现“aaaaa”。没有返回网址。

这是完整的循环:

    <?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函数接收 2 个参数:

get_the_post_thumbnail_url(int|WP_Post $post = null, string|array $size = 'post-thumbnail')

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

尝试:

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

或者简单地说:

echo get_the_post_thumbnail_url();

推荐阅读