开发者

Wordpress WP_Query

开发者 https://www.devze.com 2023-03-13 09:24 出处:网络
For some reason I cannot get attachment to display if i pass in $attachment_id if i pass a real value in like 187 it works.

For some reason I cannot get attachment to display if i pass in $attachment_id if i pass a real value in like 187 it works.

I am using WpAlchemy and Custom Image Sizes plugin.

<section id="new">
<?php $myquery = new WP_Query(array('post_type' =>开发者_JAVA百科 array('post', 'website_gallery'),'showposts' => '2'));
  while ($myquery->have_posts()) : $myquery->the_post();
  global $custom_metabox;
?>
  <div class="latest hentry">
    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <?php $website_gallery->the_meta(); ?>
    <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
      <img src="<?php $website_gallery->the_value('galleryimage');?>" alt="<?php the_title(); ?>">
    </a>
    <?php echo wp_get_attachment_image($attachment_id, '220x80'); ?>
  </div>
  <?php endwhile; wp_reset_query(); ?>
</section>


I think you can get the attachment id by using the get_post() function. get_post() requires an array for it's parameter and that array can be something like this:

$args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => $post->ID );

Since you want attachments, make sure your 'post_type' => 'attachment'.

You can then do:

$attachments = get_post( $args );
if( $attachments ) {
    foreach( $attachments as $attachment ) {
     echo wp_get_attachment_image( $attachment->ID, '220x80' );
    }
}

Adapt that code for what you need. Hopefully it'll work for you.

Check out: http://codex.wordpress.org/Template_Tags/get_posts#Show_attachments_for_the_current_post

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号