使用下面的代码:
<ul id="attachments">
<?php foreach ($attachments as $attachment) {
if ($attachment ->post_mime_type != 'image/jpeg') {
$filetipe = $attachment->post_mime_type;
$signs = array(".", ",", "-", "application", "/");
$filetype = str_replace($signs, "", $filetipe);
?>
<li class="<?php echo $filetype; ?>"><a href="<?php echo wp_get_attachment_url($attachment->ID); ?>" target="blank"><?php echo apply_filters( 'the_title', $attachment->post_title ); ?> </a></li>
<?php } } ?>
</ul>
您可以获得除图片外的所有帖子附件。现在您可以编辑您的 sidebar.php 并查询附有这些 pdf 文件的帖子。这将是您的 sidebar.php 文件的完整代码,其中 5 是您的帖子的 ID
<?php
query_posts( 'p=5' );
while ( have_posts() ) : the_post(); ?>
<?php $args = array(
'post_type' => 'attachment',
'numberposts' => null,
'posts_per_page' => 666,
'post_status' => null,
'post_parent' => $post->ID
);
$attachments = get_posts($args);
if ($attachments) {?>
<ul id="attachments">
<?php foreach ($attachments as $attachment) {
if ($attachment ->post_mime_type != 'image/jpeg') {
$filetipe = $attachment->post_mime_type;
$signs = array(".", ",", "-", "application", "/");
$filetype = str_replace($signs, "", $filetipe);
?>
<li class="<?php echo $filetype; ?>"><a href="<?php echo wp_get_attachment_url($attachment->ID); ?>" target="blank"><?php echo apply_filters( 'the_title', $attachment->post_title ); ?> </a></li>
<?php } } ?>
</ul>
<?php } ?>
<?php endwhile; ?>
<?php wp_reset_query(); ?>