从wordpress检索帖子图像

时间:2015-12-05 16:33:20

标签: php wordpress image

我的问题是,下面的代码仅为前四个帖子检索图像作为附件。对于其他人,它只检索帖子标题。并且每个帖子的每一端都是相同的,并且在数据库和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>

1 个答案:

答案 0 :(得分:0)

好了,因为你已经收到了第一个get_posts中的帖子,我不知道你为什么需要另一个帖子。

这是来自食典委的this page

====

显示当前帖子的附件[编辑] 在The Loop中进行此操作(其中$ post-&gt; 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 );
    }
}
?>