2017-08-25 59 views
0

我已經找到了一個代碼,以顯示所有媒體文件,但我怎麼列出所有的PDF文件只能用自定義字段如何使用自定義字段顯示所有PDF文件

<?php 

$args = array(
    'post_type' => 'attachment', 
    'numberposts' => -1, 
    'post_status' => null, 
    'post_parent' => null, // any parent 
    ); 
$attachments = get_posts($args); 
if ($attachments) { 
    foreach ($attachments as $post) { 
     setup_postdata($post); 
     the_title(); 
     the_attachment_link($post->ID, false); 
     the_excerpt(); 
    } 
} 

?> 

回答

1

請詳細閱讀這個答案和嘗試。

$extension = 'pdf'; 
 
$uploads = wp_upload_dir(); 
 

 
$files = new \FilesystemIterator( 
 
    $uploads['path'], 
 
    \FilesystemIterator::SKIP_DOTS 
 
    | \FilesystemIterator::FOLLOW_SYMLINKS 
 
); 
 

 
$html = []; 
 
foreach ($files as $pdf) 
 
{ 
 
    /** @noinspection PhpIncludeInspection */ 
 
    if ( 
 
     ! $files->isDir() 
 
     && $extension === $files->getExtension() 
 
    ) 
 
     $html[] = $files->getRealPath(); 
 
} 
 

 
You can then easily craft your final MarkUp using for e.g. native PHPs explode() function: 
 

 
printf(
 
    "<ul><li>%s</li></ul>", 
 
    explode("</li><li>", $html) 
 
);

幫助鏈接:

https://wordpress.stackexchange.com/questions/214515/list-and-show-uploaded-pdf-files-dynamically#answer-215516

+0

感謝,幫助了很多,但如何顯示的唯一的PDF文件具有對自定義字段值..ex: - custom_file =「文件」 –

相關問題