如何显示包含自定义字段的所有pdf文件

时间:2017-08-25 09:05:29

标签: wordpress

我找到了显示所有媒体文件的代码,但如何仅使用自定义字段列出所有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 个答案:

答案 0 :(得分: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