【问题标题】:Actual file location of a WordPress featured image?WordPress 特色图片的实际文件位置?
【发布时间】:2018-03-03 23:17:28
【问题描述】:

我正在为“垃圾箱”中的某些帖子生成查询。我可以检索特色图片 URL,但我正在寻找一种方法来获取服务器上的特色图片文件位置。

# generate the query
foreach ( $query as $post ) {
    $thumbID = get_post_thumbnail_id( $post->ID );
    $thumbObj = get_post( $thumbID );
    $source_url = $thumbObj->guid;
    # how do I get the string "/path/to/wordpress/media/library/thumbnail.jpg" ?
}

我找不到任何返回实际特色图像文件位置的 WordPress 函数。

【问题讨论】:

标签: php wordpress


【解决方案1】:
$post_thumbnail_id = get_post_thumbnail_id($post->ID);
var_dump(get_attached_file($post_thumbnail_id));

【讨论】:

    【解决方案2】:

    扩展大卫的回答,这对我有用:

    function return_image_path($thumbID) {
                        $thumbID = get_post_thumbnail_id( $post->ID );
                        $image= wp_get_attachment_image_src($thumbID);
                        $upload_dir = wp_upload_dir();
                        $base_dir = $upload_dir['basedir'];
                        $base_url = $upload_dir['baseurl'];
                        $imagepath= str_replace($base_url, $base_dir, $image[0]);
                        if ( file_exists( $imagepath) ) return $imagepath;
                        return false;
    }
    

    【讨论】:

      【解决方案3】:

      没有 wp 功能,但它很容易让您自己制作,例如

      function return_image_path($thumbID) {
      
          $image= wp_get_attachment_image_src($thumbID);
          $imagepath= str_replace(get_site_url(), $_SERVER['DOCUMENT_ROOT'], $image[0]);
      
          if($imagepath) return $imagepath;
      
          return false;
      
      }
      

      【讨论】:

      • 非常接近,尽管代码假定 WordPress 安装在根目录中。然而,它确实让我找到了具有非常相似解决方案的函数 wp_upload_dir()。
      猜你喜欢
      • 2019-07-31
      • 2015-04-28
      • 2014-11-22
      • 2012-12-12
      • 1970-01-01
      • 1970-01-01
      • 2015-07-17
      • 2014-05-13
      • 2015-04-12
      相关资源
      最近更新 更多