我希望检索附加到单个帖子的不同PDF和ZIP文件,并使用WP功能在内容之外的特定部分中显示它们get_attached_media()
. 虽然我的代码在新上传的媒体文件中正常工作,但如果一个文件已经上传并在其他帖子中使用,我无法使其正常工作。
例如,假设post ID-1嵌入file-1。内容中的pdf。然后,如果我创建post ID-2并再次插入file-1。pdf格式转换为内容,get_attached_media( \'application/pdf\', $post->ID )
将返回一个空数组?!
我是否遗漏了什么/我是否应该冲洗任何东西(如果是,如何?
以下是我的循环代码:
loop-single.php
<?php
if ( have_posts() ) : while (have_posts() ) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php
if ( has_post_thumbnail() ) {
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), \'item-featured\' )[\'0\'];
} else {
$thumb = false;
}
$content = preg_replace(\'/\\[gallery.*\\]/\', \'\', wpautop( get_the_content() ) );
$the_content_EN = get_extended(get_post_meta(get_the_ID(),\'mysecondeditor\')[\'0\'])[\'main\'];
$the_content;$date_publication;
$the_content = apply_filters(\'the_content\', $content );
// Media files ?
$pdf = get_attached_media( \'application/pdf\', $post->ID );
$zip = get_attached_media( \'application/zip\', $post->ID );
$attachments = ($pdf || $zip) ? true : false;
// DEBUG info
print_r($pdf);
print_r($zip);
?>
<div class="project">
<h3 class="entry-title">
<?php the_title(); ?>
</h3>
<div class="left">
<?php if($thumb): ?>
<span class="project-img"><img src="<?php echo $thumb; ?>" alt /></span>
<?php endif; ?>
<?php if ($attachments) : ?>
<h4>Downloads:</h4>
<ul class="project-files lino">
<?php if ($pdf) foreach($pdf as $m) : ?>
<li>
<a href="<?php echo ( wp_get_attachment_url($m->ID) ); ?>" target="_blank" class="pdf"><i class="fa fa-file-pdf-o"></i>
<?php echo ( wp_get_attachment_link($m->ID) ); ?>
</a>
</li>
<?php endforeach; ?>
<?php if ($zip) foreach($zip as $n) : ?>
<li>
<a href="<?php echo ( wp_get_attachment_url($n->ID) ); ?>" target="_blank" class="zip"><i class="fa fa-file-archive-o"></i>
<?php echo ( wp_get_attachment_link($n->ID) ); ?>
</a>
</li>
<?php endforeach; ?>
<?php endif; ?>
</ul>
</div>
<div class="entry-content right">
<?php
echo $the_content;
if ($the_content_EN) :
?>
<hr>
<section class="lang-en">
<?php echo $the_content_EN; ?>
</section>
<?php endif; ?>
</div><!-- .entry-content -->
</div>
<div class="clear"></div>
</div><!-- #post-## -->
<div class="entry-utility">
<?php edit_post_link( __( \'Edit\', \'ywp\' ), \'<span class="edit-link">\', \'</span>\' ); ?>
</div><!-- .entry-utility -->
<?php endwhile;endif;// end of the loop. ?>