此函数提供附件附加到的post ID列表。
粘贴此代码functions.php
/**
* get_attachment_parent fx gives array of post id to which given attachment attach
* @param [int $att_id attachment id
* @return array list of post ids
*/
function ravs_get_attachment_parent( $att_id ){
$args=array(
\'posts_per_page\' => -1,
);
$posts=get_posts($args);
if( ! $posts && empty($posts) )
return array();
$ancestors = array();
foreach ($posts as $post) {
$attachment = get_post_thumbnail_id($post->ID);
if( $att_id === $attachment && ! in_array($post->ID, $ancestors))
$ancestors[] = $post->ID;
}
return $ancestors;
}
并在循环中访问此函数
if( has_post_thumbnail() )
print_r( ravs_get_attachment_parent( get_post_thumbnail_id(get_the_ID())) );
或外部回路
print_r( ravs_get_attachment_parent( $attachment_id );