【问题标题】:the_post_thumbnail not workingthe_post_thumbnail 不起作用
【发布时间】:2021-07-08 21:01:40
【问题描述】:

我对 wordpress 不是很熟悉,但我一直在努力将我的布局写成一个主题,这样我就可以使用 wordpress 平台。由于某种原因,我无法使 the_post_thumbnail 功能正常工作。我已经加了

add_theme_support( 'post-thumbnails' );

到我的functions.php文件,是的,我确实设置了特色图片并包含the_post_thumbnail();在循环。

我做错了什么?我尽可能地查看了实际函数,发现在引用 wp_get_attachment_image_src() 函数时 wp_get_attachment_image() 函数中的 $image 变量存在问题。 $image 是空的,当我猜它不应该是。它得到的 post_thumbnail id 很好,所以我知道它知道有一个图像集。它只是不会显示该死的东西。

我正在从头开始为自己编写一个自定义主题,因此 functions.php 只有 add_theme_support( 'post-thumbnails' );如果你好奇,现在就在里面。

编辑:

这是我的循环:

<?php if (have_posts()) : ?>
  <?php while (have_posts()) : the_post(); ?>
    <div class="home-entry clearfix" id="post-<?php the_ID(); ?>">

      <a href="<?php the_permalink() ?>" rel="bookmark" >
        <?php
        if (has_post_thumbnail()) {
          the_post_thumbnail(); 
        } else {
          echo '<img class="home-thumb trans-border" src="' . catch_first_image() . '" width="200px" height="150px" title="' . the_title() . '" />';
        }
        ?>
      </a>

      <div class="home-post">
        <div class="home-meta">Posted <?php the_time('M j, Y'); ?> - <?php the_category(' , ') ?> - <?php comments_popup_link('No Comments', '1 Comment', '% Comments'); ?></div>
        <h2 class="post-title">
          <a href="<?php the_permalink(); ?>" title="Permanent Link to <?php the_title_attribute(); ?>" rel="bookmark" class="title"><?php the_title(); ?></a>
          <a class="read" href="<?php the_permalink(); ?>" title="Read More">Read More</a>
        </h2>

        <div class="home-excerpt">
          <?php the_excerpt(); ?>
        </div>
      </div>



    </div>

  <?php endwhile; ?>    
  <?php echo paginate_links() ?> 
<?php else : ?>
  <h2>Nothing Found</h2>
<?php endif; ?> 

续:

所以我去找了一个支持特色图片的主题,并完全复制了循环的那部分,但仍然没有:

<?php if(has_post_thumbnail()) {
echo '<span class="thumbnail"><a href="'; the_permalink(); echo'">';the_post_thumbnail(array(100,100)); echo '</a></span>';

 } else {

                  $image = evlget_first_image(); 

                    if ($image):
                  echo '<span class="thumbnail"><a href="'; the_permalink(); echo'"><img src="'.$image.'" alt="';the_title();echo'" /></a></span>';
                   endif;
           } ?>

那到底是什么?我很困惑...

【问题讨论】:

  • 你是如何使用 the_post_thumbnail() 的?当您说 $image 为空时,这表明您期望从 the_post_thumbnail 返回,而不是回声(get_* 是返回,the_* 是回声)。请使用循环中的代码进行更新,以便我们更好地了解您在做什么。
  • 以为会这样做,因为最初我是在呼应该功能。没有回声,但我仍然一无所获......
  • 一切正常,但无论在哪里 the_post_thumbnail();是空的吗

标签: wordpress wordpress-theming


【解决方案1】:

这个函数会自动回显。

解决方法:去掉函数前面的回声。

查看this

【讨论】:

  • 好的,我删除了回声,仍然没有
【解决方案2】:

您的图像是否本地存储在您的服务器/本地机器上? 如果没有,the_post_thumbnail() 将无法工作,因为它无法根据 URL 检索图像。

【讨论】:

    猜你喜欢
    • 2018-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多