【问题标题】:how to use URL of upload featured image in wordpress如何在wordpress中使用上传特色图片的URL
【发布时间】:2014-06-04 18:04:43
【问题描述】:

在 wordpress 中我正在创建一个主题,但我需要帮助,

<? the_post_thumbnail();?>

当我这样做时,它会给我整个&lt;img src=#&gt;.....

到目前为止一切都很好,但我想做一些类似的事情

<a href="UPLOADEDIMAGEURL"><? the_post_thumbnail();?></a>

因此, 我需要获取上传的图片 URL 并用锚点包装,然后替换上传的图片 URL,我该怎么做?我在 jQuery 中尝试过一些,但我想我也可以从 php 中做到这一点?

这是 jQuery 版本(因为我无法将 img src 属性替换为 href。)

$('.floatedImg').find('img').wrap( "<a href='#' class='colorbox'></a>" );

解决方案 1:可以通过 jQuery 处理 解决方案2:可以通过php

问候

【问题讨论】:

    标签: javascript php jquery html wordpress


    【解决方案1】:

    试试这个:

    <?php
    $img_url = wp_get_attachment_img_src( get_post_thumbnail_id(), 'thumbnail');
    ?>
    <a href="<?php echo $img_url[0]; ?>"><?php the_post_thumbnail();?></a>
    

    供参考:

    【讨论】:

      【解决方案2】:

      您可以在 functions.php 文件中添加类似的内容

      function get_thumbnail_url() {
          if ( has_post_thumbnail() ) {
              $thumb_url = wp_get_attachment_img_src( get_post_thumbnail_id(), 'thumbnail');
              return $thumb_url[0];    
          }
          else {
              return get_bloginfo( 'stylesheet_directory' ) . '/images/thumbnail-default.jpg';
          }
      }
      

      然后

      <a href="<?php echo get_thumbnail_url(); ?>"><? the_post_thumbnail();?></a>
      

      【讨论】:

        猜你喜欢
        • 2016-08-14
        • 2014-10-22
        • 1970-01-01
        • 2014-11-22
        • 2012-06-30
        • 1970-01-01
        • 1970-01-01
        • 2013-12-18
        • 1970-01-01
        相关资源
        最近更新 更多