要获取附件ID,您需要使用get_posts()
使用post_type
作为“附件”和post_parent
作为您感兴趣获取附件的帖子的ID。
namespace StackExchange\\WordPress;
function the_post( \\WP_Post $post, \\WP_Query $query ) {
//* Get post attachments
$attachments = \\get_posts( [
\'post_type\' => \'attachment\',
\'posts_per_page\' => -1,
\'post_parent\' => $post->ID,
\'exclude\' => get_post_thumbnail_id()
] );
//* There can be more than one attachment per post, so loop through them
foreach( $attachments as $attachment ) {
//* Maybe do some sanity checks here
$parsed = parse_url( \\wp_get_attachment_url( $attachment->ID ) );
//* Do something useful with the parsed URL
}
}
\\add_action( \'the_post\', __NAMESPACE__ . \'\\the_post\', 10, 2 );
上面,我正在使用
the_post
获取帖子父级的帖子ID的操作。根据您的用例,您可以使用另一个钩子和
get_the_ID()
获取post父级的ID。