2012-02-27 12 views
0

私のブログでは写真の添付ファイルのページがありますが、写真は一度に表示され、その2つの写真はナビゲーションとして使用され、私はそれを嫌いです。現在の投稿のWordPress添付ファイルを表示するにはどうすればよいですか?

添付ファイルのページに、そのセットの残りの部分と一緒に表示されるすべての写真が表示されます。ここで

は、私はそれがすべてのポストの添付ファイルを表示するように変更するにはどうすればよいの現在のコード

 <div id="nav-images" class="navigation clearfix"> 
      <div class="nav-next"><?php next_image_link() ?></div> 
      <div class="nav-previous"><?php previous_image_link() ?></div> 

のですか?あなたはページまたはポストの上にいるとき、あなたは次のようにそのすべての添付ファイルを取得することができます

答えて

3

global $post; // refers to the post or parent being displayed 
$attachements = query_posts(
    array(
    'post_type' => 'attachment', // only get "attachment" type posts 
    'post_parent' => $post->ID, // only get attachments for current post/page 
    'posts_per_page' => -1  // get all attachments 
) 
); 
foreach($attachements as $attachment){ 
    // Do something exceedingly fancy 
} 

添付ファイルのページで、現在しているので、あなたが使用して他のすべての添付ファイルを取得することができます$post->post_parent値:

global $post; // refers to the attachement object 
$attachements = query_posts(
    array (
    'post_type' => 'attachment', // only get "attachment" type posts 
    'post_parent' => $post->post_parent, // attachments on the same page or post 
    'posts_per_page' => -1  // get all attachments 
) 
); 

その後、添付ファイルの画像を表示するためには、あなたはwp_get_attachment_image_src機能を使用することができます。添付ファイルのIDは、foreachループの各繰り返しで$attachement->ID(最初の例と同じ命名規則を使用している場合)として使用できます。

5

明確にするために、これはもう動作しません。少なくとも、バージョン3.5.2ではこれ以上は機能しません。私は代わりにこれを使用しました。

$attachments = get_children(
    array(
    'post_type' => 'attachment', 
    'post_parent' => get_the_ID() 
) 
); 
foreach ($attachments as $attachment) { 
    // ... 
} 

この検索用語では非常に高いランク付けされているため、古いスレッドのみを復活させます。

+0

簡単に栄光! – oles

0

ワードプレス3.6.0以来、get_attached_mediaも使用できます。

$media = get_attached_media('image', $post->ID); 
if(! empty($media)){ 
    foreach($media as $media_id => $media_file){ 
     $thumbnail = wp_get_attachment_image_src ($media_id, 'thumbnail'); 
     $full = wp_get_attachment_url($media_id); 
     echo '<a href="'.$full.'" target="_blank"><img src="'.$thumbnail[0].'" alt="'.$media_file->post_title.'" /></a>'; 
    } 
} 
関連する問題