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