web-dev-qa-db-fra.com

Comment obtenir des pièces jointes pour un type de message spécifique?

J'ai cette page où je montre toutes les pièces jointes de l'article.

C'est le code:

<?php
$args = array(
    'post_type' => 'attachment',
    'post_status' => 'published',
    'posts_per_page' =>25,
    'post_parent' => null, // Post-> ID;
    'numberposts' => null,
);

$attachments = get_posts($args);

$post_count = count ($attachments);

if ($attachments) {
    foreach ($attachments as $attachment) {
    echo "<div class=\"post photo col3\">";
        $url = get_attachment_link($attachment->ID);// extraigo la _posturl del attachmnet      
        $img = wp_get_attachment_url($attachment->ID);
        $title = get_the_title($attachment->post_parent);//extraigo titulo
        echo '<a href="'.$url.'"><img title="'.$title.'" src="'.$img.'"></a>';
        echo "</div>";
    }   
}
?>

Maintenant, j'ai les post_types 'post', 'page' et 'videos'. Je les reçois tous, mais je souhaite afficher uniquement "post" post_type. Comment exclure 'video' post_type?

Merci!

4
user1341839
$posts = get_posts( array( 'post_type' => 'post', 'posts_per_page' => -1 ) );

     foreach ( $posts as $post ) {

         $attachments = get_children( array (
            'post_parent'    => $post->ID,
            'post_type'      => 'attachment',
            'post_mime_type' => 'image',
            'post_per_page'  => 25
        ), ARRAY_A );


      if( $attachments ) {
         //continue with what you were doing

      }
   }
1
Chris_O

il suffit de coller le code suivant n'importe où dans votre fichier de publication et les pièces jointes seront affichées.

$ args = array (
 'post_type' => 'pièce jointe', 
 'numberposts' => null, 
 'post_status' => null, 
 'post_parent '=> $ post-> ID 
); 
 $ attachments = get_posts ($ args); 
 if ($ attachments) {
 pour chaque ($ attachments as $ attachment) {
 echo apply_filters ('the_title' , $ attachment-> post_title); 
 le_attachement_lien ($ attachment-> ID, false); 
} 
} 
0
Selva Balaji