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 php的curl不支持http2
  • ¥15 为什么我按照电路图做出的仿真和实物都不能使用
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面
  • ¥15 算法题:数的划分,用记忆化DFS做WA求调
  • ¥15 chatglm-6b应用到django项目中,模型加载失败
  • ¥15 CreateBitmapFromWicBitmap内存释放问题。
  • ¥30 win c++ socket
  • ¥15 C# datagridview 栏位进度
  • ¥15 vue3页面el-table页面数据过多