Wordpress 使用此附件将目标设置为空

Wordpress 使用此附件将目标设置为空,wordpress,media,attachment,Wordpress,Media,Attachment,下面的php将像这样打印,带有锚文本“下载” <?php if ( $attachments = get_children( array( 'post_type' => 'attachment', 'post_mime_type'=>'image', 'numberposts' => 1, 'post_status' => null, 'post_parent' => $post->ID ))); foreach ($att

下面的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.当用户单击此链接时,如何在空白中定位或在新选项卡中打开

2.这段短代码是否可以与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>';
}
if($attachments=get_posts(数组)(
“post_类型”=>“附件”,
“post_mime_type”=>“image”,
“numberposts”=>-1,
“post_状态”=>“任何”,
'post_parent'=>$post->ID,
) ) );
foreach($attachments作为$attachment){
回声';
}

这就是我在下面的回答中的意思

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

看看这样行不行

amek teh 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>';
foreach($attachments as$attachment){
回声';
}


希望这对您有所帮助

为什么不尝试为您的阵列包含一个目标?数组('post\u type'=>'附件','post\u mime\u type'=>'图像','numberposts'=>-1,'post\u状态'=>'任何','post\u父项'=>$post->ID,'target'=>''.\u blank')查看它是否可以这样工作。这不是错误,但不会在新选项卡中打开,与上面的代码相同。您在什么时候收到此错误?如果你不按我加的路线跑,你会得到什么?您能否通读get_children()函数,然后进行编辑以适应数组中的“目标”。可以然后将其更改为'target'=>'target=“_blank”';我认为它现在应该能够将其打印为target=“_blank”,而不仅仅是在没有目标标记added的情况下打印为_blank。您编辑的代码也可以工作,但我使用的代码看起来像这样,也可以工作<代码>回显“”顺便说一句,非常感谢。耶!这将在新选项卡中打开。。但不下载图片。hohoonclick=“document.execCommand(\'SaveAs\',true,\''.get\u permalink($attachment->ID)。'\');window.open(this.href);return false;“将其替换为代码中的