我有附件。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 This Wallpaper\');
}
?>
此代码将打印附件链接。
我的问题是:如何使这个链接成为一次点击下载图像并保存给计算机用户?
SO网友:tfrommen
虽然它不是特定于WP的,但以下是如何强制用户下载图像:
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>\';
}
Note: 代码未经测试。