如何获得产品图片的绝对路径或任何帖子/产品页面的精选图片?
我试着这样:
$product = wc_get_product( $postid);
$product->get_image('large'); //output like <img src="url of image" alt="xyz"/>
然后我尝试使用php prag_match函数从图像标记(从上面的输出)获取src路径。 然后通过域url拆分并获得剩余路径并创建绝对路径。
$filename = getcwd().'/wp-content/uploads/2016/02/xyz.jpg';
它适用于我,但似乎很复杂和管理的东西。如果有最简单的方法,请告诉我?
答案 0 :(得分:3)
<?php
$feat_image = wp_get_attachment_url( get_post_thumbnail_id( $post->ID ) );
echo( $feat_image );
?>
https://wordpress.org/support/topic/getting-a-post-featured-image-url
https://wordpress.org/support/topic/retrieveing-featured-image-url
https://codex.wordpress.org/Function_Reference/wp_get_attachment_url
为了获得系统路径,Konstantin Kovshenin建议如下:
$url = wp_get_attachment_url( $post_ID );
$uploads = wp_upload_dir();
$file_path = str_replace( $uploads['baseurl'], $uploads['basedir'], $url );
https://kovshenin.com/2011/attachments-filename-and-directory-in-wordpress/
https://developer.wordpress.org/reference/functions/wp_upload_dir/