Então, se você começar com isso:
$all_images = get_posts( array(
'post_type' => 'attachment',
'numberposts' => -1,
) );
Em seguida, $all_images
é uma matriz de objetos . Percorra cada um deles:
foreach ( $all_images as $image ) {}
Dentro desse foreach, você pode usar os parâmetros normais disponíveis para o objeto $post
:
-
$image->ID
é o ID da postagem do anexo -
$image->post_parent
é o ID da postagem pai da postagem do anexo
Então, vamos usar isso, para conseguir o que você procura, usando get_the_title()
e get_permalink()
:
// Get the parent post ID
$parent_id = $image->post_parent;
// Get the parent post Title
$parent_title = get_the_title( $parent_id );
// Get the parent post permalink
$parent_permalink = get_permalink( $parent_id );
Isso é muito bonito!
Colocando tudo junto:
<?php
// Get all image attachments
$all_images = get_posts( array(
'post_type' => 'attachment',
'numberposts' => -1,
) );
// Step through all image attachments
foreach ( $all_images as $image ) {
// Get the parent post ID
$parent_id = $image->post_parent;
// Get the parent post Title
$parent_title = get_the_title( $parent_id );
// Get the parent post permalink
$parent_permalink = get_permalink( $parent_id );
}
?>