2013-03-16 69 views
1

低於這個PHP將打印這樣http://example.com/wp-content/uploads/2013/01/imagename.jpg的錨文本「下載」使目標_blank隨着這Attacment PHP

<?php 

if ($attachments = get_children(array( 
'post_type' => 'attachment', 
'post_mime_type'=>'image', 
'numberposts' => 1, 
'post_status' => null, 
'post_parent' => $post->ID 
))); 
foreach ($attachments as $attachment) { 
echo wp_get_attachment_link($attachment->ID, '' , false, true, 'Download'); 
} 
?> 

1.當用戶點擊這個鏈接,如何_blank或在新標籤中打開目標。

2.is可能這個短代碼結合Javascript來製作強制下載鏈接?看起來像波紋管。

if ($attachments = get_posts(array(
    'post_type' => 'attachment', 
    'post_mime_type'=>'image', 
    'numberposts' => -1, 
    'post_status' => 'any', 
    'post_parent' => $post->ID, 
))); 
foreach ($attachments as $attachment) { 
    echo '<a href="javascript:void(0);" 
     onclick="document.execCommand(\'SaveAs\', true, \'' . get_permalink($attachment->ID) . '\');"> 
     Download This Wallpaper</a>'; 
} 

回答

0

這就是我在迴應中的含義。

array( 
'post_type' => 'attachment', 
'post_mime_type'=>'image', 
'numberposts' => 1, 
'post_status' => null, 
'post_parent' => $post->ID , 
'target' => 'target="_blank"'; 
) 

看看它是否以這種方式工作。

+0

解析錯誤: '' 語法錯誤,意外 – 2013-03-16 04:07:10

+0

感謝好友.. :) – 2013-03-16 04:15:08

+0

爲什麼不嘗試爲你的數組包含一個目標? 陣列( 'post_type'=> '附着', 'post_mime_type'=> '圖像', 'numberposts'=> -1, 'post_status'=> '任意', 'post_parent'=> $交 - > ID, 'target'=>'_blank' ) 看看它是否以這種方式工作。 – OmniPotens 2013-03-16 04:22:16

0

AMEK德chages和嘗試這個

foreach ($attachments as $attachment) { 
echo '<a href="javascript:void(0);" 
    onclick="document.execCommand(\'SaveAs\', true, \'' . get_permalink($attachment->ID) . '\');" target="blank"> 
    Download This Wallpaper</a>'; 

}

希望這將helpt你

+0

你好!這在新標籤打開..但不下載圖像。 hoho – 2013-03-16 04:24:45

+0

onclick =「document.execCommand(\'SaveAs \',true,\''。get_permalink($ attachment-> ID)。'\'); window.open(this.href); return false;」用代碼替換它 – 2013-03-16 05:04:58