【问题标题】:how to display posts image from wordpress blog in html/php page如何在 html/php 页面中显示来自 wordpress 博客的帖子图像
【发布时间】:2021-10-07 04:31:37
【问题描述】:

以下是我在 html/php 页面中显示博客最近帖子的代码

    <div class="row">
     <?php
    
        include('blog/wp-load.php'); 
        $recent_posts = wp_get_recent_posts(array(
          'numberposts' => 3,
          'post_type' => 'post',
          'post_status' => 'publish'
        ));
        foreach($recent_posts as $post) {
          echo '<div class="col-lg-4">';
          echo '<div class="blog-entry">';
          echo '<a href="', get_permalink($post['ID']), '" style="background-image: url(images/BlogImg.png);"></a>';
          echo '<div class="text">';
          echo '<div class="meta">';
          echo '<div><span class="fa fa-calendar-alt"></span>', $post['post_date'], '</a></div>';
          echo '<div><span class="fa fa-user"></span>', $post['post_author'], '</div>';
          echo '<div><span class="fa fa-comments meta-chat"></span>', $post['comment_count'], '</div>';
          echo '</div>';
          echo '<div class="desc">';
          echo '<h3 class="heading"><a href="', get_permalink($post['ID']), '">', $post['post_title'], '</a></h3>';
          echo '</div>';
          echo '</div>';
          echo '</div>';
          echo '</div>';
        }
    
    ?>
  </div>

如何使用background-image: url(images/BlogImg.png);显示帖子图片

代替images/BlogImg.png,我想要发布相应博客文章的图片网址。

谁能告诉我如何才能做到这一点?

【问题讨论】:

    标签: php html wordpress


    【解决方案1】:

    使用以下行:

    <div class="row">
        <?PHP
    
            include('blog/wp-load.php'); 
            $recent_posts = wp_get_recent_posts(array(
              'numberposts' => 3,
              'post_type' => 'post',
              'post_status' => 'publish'
            ));
            foreach($recent_posts as $post) {
       $imgurl = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'thumbnail' );
    
              echo '<div class="col-lg-4">
                    <div class="blog-entry">
                    <a href="'. get_permalink($post['ID']). '" style="background-image: url('.$imgurl.');"></a>
               <div class="text">
               <div class="meta">
               <div><span class="fa fa-calendar-alt"></span>' . $post['post_date'] . '</a></div>
              <div><span class="fa fa-user"></span>'. $post['post_author'] . '</div>
              <div><span class="fa fa-comments meta-chat"></span>' . $post['comment_count'] . '</div>'
              </div>
              <div class="desc">
              <h3 class="heading"><a href="' . get_permalink($post['ID']) . '">' . $post['post_title'] . '</a></h3>
              </div>
              </div>
              </div>
              </div>';
            }
        
        ?>
    </div>
    

    【讨论】:

      【解决方案2】:

      根据您存储图片的方式,如果这是附加到您帖子的特色图片,请添加此

      $backgroundImg = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'full');

              foreach($recent_posts as $post) {
                $backgroundImg = wp_get_attachment_image_src(get_post_thumbnail_id($post['ID']), 'full' );
                echo '<div class="col-lg-4">';
                echo '<div class="blog-entry">';
                echo '<a href="', get_permalink($post['ID']), '" style="background-image: url('<?php echo $backgroundImg[0]; ?>');"></a>';
                echo '<div class="text">';
                echo '<div class="meta">';
                echo '<div><span class="fa fa-calendar-alt"></span>', $post['post_date'], '</a></div>';
                echo '<div><span class="fa fa-user"></span>', $post['post_author'], '</div>';
                echo '<div><span class="fa fa-comments meta-chat"></span>', $post['comment_count'], '</div>';
                echo '</div>';
                echo '<div class="desc">';
                echo '<h3 class="heading"><a href="', get_permalink($post['ID']), '">', $post['post_title'], '</a></h3>';
                echo '</div>';
                echo '</div>';
                echo '</div>';
                echo '</div>';
              }
      

      【讨论】:

      • @Steve 如果图像存储在上传文件夹中,这会起作用吗?
      • 你是如何将图片分配给帖子的?
      • @Steve 从精选图片部分上传
      • 那么,如果您将它们指定为特色图片,那么这应该可以正常工作
      • @Steve 它的工作.. 谢谢.. 我还有一个问题.. 如何根据类别显示帖子
      猜你喜欢
      • 1970-01-01
      • 2023-03-27
      • 2017-04-29
      • 1970-01-01
      • 2019-05-28
      • 2017-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多