有两个函数可以根据彼此获取不同的附件参数,但粗略看一下并不能表明有一个函数可以根据附件页面URL获取附件参数。
您可以尝试使用$wpdb
类,然后将该ID放入wp_get_attachment_image_src()
作用像这样的事情会让你开始:
function get_attachment_id_by_url( $url ){
global $wpdb;
$url = trim( $url, \' /\' ); // Trim whitespace and trailing slash
$parts = explode( \'/\', $url );
$name = end( $parts ); // We only want the last part
if( ! $result = $wpdb->get_row( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_name = %s", $name ) ) ) // potentially replace post_name with post_title
return false;
return $result->ID;
}
这将允许您执行以下操作:
// Attachment URL we want to utilize
$image_url = \'https://example.com/attachment/name_of_photo/\';
// Get the ID using our function above
$image_id = get_attachment_id_by_url( $image_url );
// Get the SRC parameters from that ID (use whatever `$size` args you want)
$image_src = wp_get_attachment_image_src( $image_url, \'full\' );
/**
* var_dump($image_src) Output:
*
* array(4) {
* [0]=> string(48) "https://example.com/attachment/name_of_photo.png"
* [1]=> int(640)
* [2]=> int(480)
* [3]=> bool(true)
* }
*/