如何使用自定义字段显示所有PDF文件

问题描述:

我已经找到了一个代码,以显示所有媒体文件,但我怎么列出所有的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(); 
    } 
} 

?> 

请详细阅读这个答案和尝试。

$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 =“文件” –