2013-03-16 36 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