2017-02-22 1 views
-2

나는 이미지를 배포하는 개인 WordPress 사이트에서 일하고 있습니다.Wordpress에서 여러 이미지를 다운로드하십시오.

단일 게시물 페이지에서 ACF (Advanced Custom Fields)를 통해 여러 이미지를 등록했으며 ACF 기능을 통해 get_field ACF 기능을 통해 이미지 경로/파일 이름을 얻는 방법을 알고 있습니다.

나는 그저이 페이지를 찾았지만,이 기술을 어떻게 WordPress 사이트에 적용 할 수 있습니까? Download multiple images into zip

지금은 붙어입니다 ... 단일 포스트 페이지에서

+0

다른 곳으로 가서 묻고있는 것을 알아 내려고 묻는 질문은 적절하지 않습니다. 코드 관련 문제가있는 경우 여기 ** 관련 ** 코드를 게시물 자체에 게시하고 해당 코드와 관련된 특정 질문을하십시오. [ask] 및 [mcve]를 참조하십시오. –

+0

어디서 붙어 있는지 정확히 설명해야합니다. –

+0

당신은 이미지 검색을위한 사용자 정의 필드 코드를 제공 할 수 있습니다. 단지 리피터 이미지 하위 필드 또는 다른 이미지 사용자 정의 필드에 있는지 확인하기 만하면 필요한 코드를 제공 할 수 있습니다. –

답변

0

, 당신은 우편을 만들기위한 모든 코드를 배치합니다 download_zip.php 파일의 URL을 배치합니다. 단일 포스트 페이지에서

:

변수 'model_id'에서
<a href="<?php echo site_url().'/download_zip.php?model_id='.$post->ID; ?>">Download ZIP</a> 

는, 단일 게시물의 게시물 ID를 배치합니다.

이제 wordpress setup의 루트에 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 당신이 $index_file 변수에 이미지의 경로를 정의하는 $explode 변수에 url을 깰 수 있도록 내부 &는 업로드 디렉토리를 정의합니다.

그리고 에 저장된 destination path 변수가 올바른지 확인하십시오.

희망, 도움이 될 수 있습니다.

+0

팁 주셔서 감사합니다! 나는 그것이 나를 위해 일하는 지 알게하려고 노력할 것이다! –

+0

WordPress 코어에 파일을 추가하는 것은 나쁜 습관입니다. 더 나은 방법은 download.php를 테마 루트 폴더에 놓고 site_url 대신 https://developer.wordpress.org/reference/functions/get_template_directory_uri/로 호출하는 것입니다. – user2685224