我正在我个人的wordpress网站上发布图片。在Wordpress中下载zip文件中的多个图像
在单篇文章页面中,我通过ACF(高级自定义字段)注册了几个图像,并且我知道如何通过get_field
ACF函数获取图像路径/文件名。
我刚刚搜索了一下,然后找到了这个页面,但是我怎么能把这个技术应用到wordpress站点呢? Download multiple images into zip
现在我坚持......
我正在我个人的wordpress网站上发布图片。在Wordpress中下载zip文件中的多个图像
在单篇文章页面中,我通过ACF(高级自定义字段)注册了几个图像,并且我知道如何通过get_field
ACF函数获取图像路径/文件名。
我刚刚搜索了一下,然后找到了这个页面,但是我怎么能把这个技术应用到wordpress站点呢? Download multiple images into zip
现在我坚持......
在单篇文章页面,将download_zip.php
文件,将放置所有的代码创建ZIP的URL。
在单后页:
<a href="<?php echo site_url().'/download_zip.php?model_id='.$post->ID; ?>">Download ZIP</a>
在可变'model_id'
,将单后的帖子ID。
现在在您的wordpress安装程序的根目录下创建一个download_zip.php
文件,其中wp-config.php文件存在。
这里是download_zip.php
文件的代码。
<?php
/*File for downloading the gallery zip files*/
$post_id = $_GET['model_id'];
require_once('wp-blog-header.php');
require_once('/wp-admin/includes/file.php');
WP_Filesystem();
$files_to_zip = array();
$zip = new ZipArchive();
$title = get_the_title($post_id);
$destination = wp_upload_dir();
//var_dump($destination);
$destination_path = $destination['basedir'];
$DelFilePath = str_replace(" ","_",$title).'_'.$post_id.'_'.time().'.zip' ;
$zip_destination_path = $destination_path."/".$DelFilePath;
if(file_exists($destination_path."/".$DelFilePath)) {
unlink ($destination_path."/".$DelFilePath);
}
if ($zip->open($destination_path."/".$DelFilePath, ZIPARCHIVE::CREATE) != TRUE) {
die ("Could not open archive");
}
//this is only for retrieving Repeater Image custom field
$row1 = get_field('acf_field_name1',$post_id);
$row1 = get_field('acf_field_name2',$post_id);
$rows = array($row1,$row2);
if($rows) {
foreach($rows as $row): ?>
<?php
$explode = end(explode("uploads",$row));
$index_file = array($destination_path,$explode);
$index_file = implode("",$index_file);
$new_index_file = basename($index_file);
$zip->addFile($index_file,$new_index_file);
endforeach;
}
$zip->close();
if(file_exists($zip_destination_path)) {
send_download($zip_destination_path);
}
//The function with example headers
function send_download($file) {
$basename = basename($file);
$length = sprintf("%u",filesize($file));
header($_SERVER['SERVER_PROTOCOL'].' 200 OK');
header('Content-Description: File Transfer');
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename="'.$basename.'"');
header('Content-Transfer-Encoding: binary');
header('Pragma: public');
header('Content-Length: ' . $length);
set_time_limit(0);
ob_clean();
flush();
readfile($file); // "Outputs" the file.
unlink($file);
}
?>
根据您的要求,如get_field()
,将请修改这个代码的image field name
里面,&定义上传目录,这样就可以在$explode
变量打破url
在$index_file
变量定义图像的路径。
并请检查您的destination path
存储在$destination_path
变量是否正确。
希望,这可能对你有所帮助。
感谢您的提示!我会尽力让你知道它是否适合我! –
将文件添加到WordPress核心是个不好的做法。更好的方法是将download.php放在主题根文件夹中,并使用https://developer.wordpress.org/reference/functions/get_template_directory_uri/而不是site_url来调用它 – user2685224
问题要求我们去别的地方试图找出你要问的东西在这里不合适。如果您在使用代码时遇到问题,请在此处发布相关的**代码,然后提出与该代码相关的具体问题。见[问]和[mcve]。 –
您需要准确解释卡住的位置。 –
您能否请您提供您的自定义字段代码来检索图像,我只是想检查他们是否在中继器图像子字段或不同的图像自定义字段,以便我可以为您提供所需的代码。 –