douchao0358 2014-09-10 14:50
浏览 108
已采纳

Wordpress自定义帖子类型 - data-thumb data-src图像

I have an Html page in which There is a slider. here's the html code

<div data-thumb="images/slide1.jpg" data-src="images/slide1.jpg">

        </div>
        <div data-thumb="images/slide2.jpg" data-src="images/slide2.jpg">

        </div>
        <div data-thumb="images/slide3.jpg" data-src="images/slide3.jpg">

        </div>

I am trying to convert this page into wordpress. Here's how I am doing this in wordpress to load the images.

  <?php 

          $options = array(
        'post_type' => "slideshow",

         );  



      while ($my_query->have_posts()) : $my_query->the_post(); ?>  


         <div data-thumb="<?php if ( has_post_thumbnail() ) { the_post_thumbnail('full'); } ?>"  data-src="<?php if ( has_post_thumbnail() ) { the_post_thumbnail('full'); } ?>">

        </div>

        <?php endwhile; ?> 

But It is not working.My pictures are not showing in the slider. But When I check the page source It is correctly getting the image.

here's the source that generates

 <div data-thumb="<img width="1170" height="385" src="http://localhost/wordpress/wp-content/uploads/2014/09/slide1.jpg" class="attachment-full wp-post-image" alt="slide1" />"  
   data-src="<img width="1170" height="385" src="http://localhost/wordpress/wp-  content/uploads/2014/09/slide1.jpg" class="attachment-full wp-post-image" alt="slide1" />">

        </div>
  • 写回答

1条回答 默认 最新

  • dongyan5706 2014-09-10 15:02
    关注

    Change this: In the first line of your "while" loop insert this:

    if ( has_post_thumbnail() ) {
         $image = wp_get_attachment_image_src( get_post_thumbnail_id( the_ID()));
    }
     // else {
     // you should see what to do if the post hasn't got a thumbnail
     // 
    

    And then replace this:

    <div data-thumb="<?php if ( has_post_thumbnail() ) { the_post_thumbnail('full'); } ?>"
    

    for this:

    <div data-thumb="<?php echo $image[0];?>"
    

    Same thing in your second "if". it's not the prettiest code, but it should work.

    Calling the_post_thumbnail() prints the whole <img> tag, and you need only the URL to populate the DATA-SRC and DATA-THUMB attributes of your existing IMGs.

    So you get the post thumbnail ID with get_post_thumbnail_id(), and get the image url with wp_get_attachment_src().

    Hope it helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大