正在从wordpress检索帖子图像


Retrieving post images from wordpress

我的问题是,下面的代码只将前四篇文章的图像作为附件检索。对于其他人,它只检索帖子标题。每个帖子的每一端都是相等的,并以相同的方式存储在数据库和wordpress网站的后端。它检索了前四个,我想是第16个帖子的图片。

    $myposts = get_posts(array(
        'category' => $_POST["kategorija"], 
        'post_type' => 'post',
        'posts_per_page' => -1
        )
  );
?>
<ul>
<?php
foreach ( $myposts as $post ) : setup_postdata( $post ); 
$title = $post->post_title;
$date = $post->post_date;
$content = $post->post_content;
$status = $post->post_status; 
?>
    <li>
<?php
 $args = array(
   'post_type' => 'attachment',
   'numberposts' => -1,
   'post_status' => null,
   'post_parent' => $post->ID
  );
  $attachments = get_posts( $args );
     if ( $attachments ) {
        foreach ( $attachments as $attachment ) {
           echo wp_get_attachment_image( $attachment->ID, 'thumbnail' );
          }
     }
     ?>
    <h2><?php echo $title; ?> </h2>
     <form enctype="multipart/form-data" action="oglas.php" method="POST">
      <input type="hidden" name="idKategorije" value="<?php echo $post->ID; ?>" />
      <input type="submit" value="selektuj" />
      </form>
    </li>
<?php
endforeach; ?> 
</ul>

既然你已经在第一个get_posts中得到了帖子,我不需要;我不明白你为什么需要另一个。

这是来自Codex:的这一页

===

显示当前文章的附件[编辑]在The Loop中执行此操作($post->ID可用)。

<?php
$args = array( 'post_type' => 'attachment', 'posts_per_page' => -1, 'post_status' =>'any', '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 );
    }
}
?>