【问题标题】:Why is my <?php the_post_thumbnail(); ?> not bringing up any images on my wordpress page?为什么我的 <?php the_post_thumbnail(); ?> 没有在我的 wordpress 页面上显示任何图像?
【发布时间】:2017-04-05 17:01:20
【问题描述】:

我的代码是

<?php
// Template Name: homepage
get_header(); ?>
    <div id="content" class="full-width">

<?php 
    query_posts(array( 
        'post_type' => 'avada_portfolio',
        'showposts' => 2 
    ) );  
?>
<?php while (have_posts()) : the_post(); ?>
        <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>


    <img src=" <?php the_post_thumbnail('medium'); ?>">
        <p><?php echo get_the_excerpt(); ?></p>
 <?php
    endwhile; //resetting the page loop
    wp_reset_query(); //resetting the page query
    ?>


<h1 class="entry-title"><?php the_title(); ?></h1> <!-- Page Title -->
    <?php
    // TO SHOW THE PAGE CONTENTS
    while ( have_posts() ) : the_post(); ?> <!--Because the_content() works only inside a WP Loop -->
        <div class="entry-content-page">
            <?php the_content(); ?> <!-- Page Content -->
        </div><!-- .entry-content-page -->

    <?php
    endwhile; //resetting the page loop
    wp_reset_query(); //resetting the page query
    ?>

<?php get_footer(); ?>

我已将&lt;?php add_theme_support( 'post-thumbnails' ); ?&gt; 添加到函数中,但它只是在我的主页上显示一个损坏的图像:(

我所有的图片都上传到我的 wordpress 网站上,并在自定义帖子上设置为特色图片!

谢谢

【问题讨论】:

  • the_post_thumbnail('medium');写出图片标签

标签: wordpress-theming wordpress


【解决方案1】:

the_post_thumbnail('medium') 这个函数将返回带有特征图像的img tag

所以你可以通过以下两种方式获取图像:-

<img src=" <?php echo wp_get_attachment_url(get_post_thumbnail_id( $post->ID ), 'medium' ); ?>">

就是这个函数:- &lt;?php the_post_thumbnail('medium'); ?&gt;

希望这会对你有所帮助。

【讨论】:

    【解决方案2】:
    <img src=" <?php the_post_thumbnail('medium'); ?>">
    

    这在&lt;img src=""&gt; 中不起作用。对我来说这很有效:

    <img src=" <?php echo wp_get_attachment_url(get_post_thumbnail_id( get_the_ID() ), 'thumbnail' ); ?>" class="media-object" style="width:73px; height:73px">
    

    【讨论】:

      【解决方案3】:

      写“the_post_thumbnail('medium');”图片标签外

      像这样:

      <?php while (have_posts()) : the_post(); ?>
          <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
      
          <?php the_post_thumbnail('medium'); ?>
          <p><?php echo get_the_excerpt(); ?></p>
      
      <?php
          endwhile; //resetting the page loop
          wp_reset_query(); //resetting the page query
      ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-07-26
        • 2013-02-07
        • 1970-01-01
        • 2014-10-25
        • 2021-06-01
        • 1970-01-01
        • 2021-02-26
        • 2021-09-28
        相关资源
        最近更新 更多