如何获取特定帖子类型的附件?

时间:2012-07-24 作者:user1341839

I have this page where I am showing all post\'s attachments.

代码如下:

<?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>";
    }   
}
?>
现在,我有了“post”、“page”和“videos”post\\u类型。我正在获取所有这些内容,但我只想显示“post”post\\u类型。如何排除“视频”post\\u类型?

谢谢

2 个回复
SO网友:Chris_O
$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

      }
   }
SO网友:Selva Balaji

只需将以下代码粘贴到帖子文件的任何位置,就会显示附件。

$args = array(
    \'post_type\' => \'attachment\',
    \'numberposts\' => null,
    \'post_status\' => null,
    \'post_parent\' => $post->ID
); 
$attachments = get_posts($args);
if ($attachments) {
    foreach ($attachments as $attachment) {
        echo apply_filters(\'the_title\', $attachment->post_title);
        the_attachment_link($attachment->ID, false);
    }
}

结束

相关推荐

Grouping post-types in loop

当用户从下拉列表中选择特定类别时,将显示多个帖子类型的列表。我想用标题中的帖子类型标题分组显示数据。我不确定如何划分为岗位类型组。我花了一点时间寻找,但没有找到我真正需要的东西。我还试图添加<?php $post_type = get_post_type( $post->ID ); echo $post_type; ?> 但这只是重复(显然)。调用循环<div id=\"content\" role=\"main\" style=\"float:right; width:765px