Advertisement
dalbeck

Get media attachments from specific tag

Sep 26th, 2013
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.67 KB | None | 0 0
  1. <?php
  2.     $args = array(
  3.         'post_type' => 'attachment',
  4.         'posts_per_page' => -1,
  5.         'post_status' => 'any',
  6.         'post_parent' => null,
  7.         'tax_query' => array(
  8.             array(
  9.                 'taxonomy' => 'post_tag',
  10.                 'field' => 'slug',
  11.                 'terms' => 'logo'
  12.             )
  13.         )
  14.     );
  15.  
  16. $images = get_posts( $args );
  17. if ( $images ) {
  18.     foreach ( $images as $image ) {
  19.         // Get the parent post ID
  20.         $parent_id = $image->post_parent;
  21.         // Get the parent post Title
  22.         $parent_title = get_the_title( $parent_id );
  23.         // Get the parent post permalink
  24.         $parent_permalink = get_permalink( $parent_id );
  25.         // Get image caption
  26.  
  27.         $logoimg = wp_get_attachment_image( $image->ID, 'Work Gallery' );
  28.  
  29.         $posttags = get_the_tags();
  30.             if ($posttags) {
  31.                 foreach ($posttags as $tag) {
  32.                     str_replace('-','_',$tag->slug . ' ');
  33.                 }
  34.             }
  35.  
  36.             echo '<aside class="work_item" data-id="id-' . get_the_ID() . '" data-type="' . $posttags . '">';
  37.  
  38.                 echo '<ul>';
  39.  
  40.                     echo '<li>';
  41.  
  42.                         echo '<div class="img_wrap">';
  43.  
  44.                             echo $logoimg;
  45.  
  46.                         echo '</div>';
  47.  
  48.                         echo '<ul class="work_meta">';
  49.  
  50.                             echo '<li class="work_title">';
  51.  
  52.                                 echo ' ' . $parent_title . ' ';
  53.  
  54.                             echo '</li>';
  55.  
  56.                             echo '<li class="work_item_content">';
  57.  
  58.                                 echo ' ' . get_post( get_post_thumbnail_id($post->ID) )->post_excerpt . ' ';
  59.  
  60.                             echo '</li>';
  61.  
  62.                         echo '</ul>';
  63.  
  64.                         echo '<ul class="work_features">';
  65.  
  66.                             echo '<li class="lightbox">';
  67.  
  68.                                 $attachment_id = $post->ID; // dynamic attachment ID
  69.                                 $image_attributes = wp_get_attachment_image_src( $attachment_id, 'full' ); // returns an array
  70.  
  71.                                 echo '<a class="work_lb tooltip" data-id="' . get_the_ID() . '" title="view in lightbox" href="' . $image_attributes[3] . '" rel="prettyPhoto[mixed]">';
  72.  
  73.                                     echo 'Lightbox';
  74.  
  75.                                 echo '</a>';
  76.  
  77.                             echo '</li>';
  78.  
  79.                         echo '</ul>';
  80.  
  81.                     echo '</li>';
  82.  
  83.                 echo '</ul>';
  84.  
  85.             echo '</aside>';
  86.  
  87.         }
  88.         wp_reset_postdata();
  89.     }
  90. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement