Deseo recuperar los diferentes archivos PDF y ZIP adjuntos a una sola publicación y mostrarlos en una sección específica, fuera del contenido, utilizando la función WP get_attached_media()
. Si bien mi código funciona como debería para los archivos de medios recién cargados, no puedo lograr que funcione si ya se ha cargado un archivo y se ha utilizado en otra publicación.
Por ejemplo, supongamos que la publicación ID-1 incrusta file-1.pdf en el contenido. Luego, si creo la publicación ID-2 y vuelvo a insertar file-1.pdf en el contenido, get_attached_media( 'application/pdf', $post->ID )
devolverá una matriz vacía.!
¿Me estoy perdiendo algo? ¿Debo limpiar algo (si es así, cómo ...)?
Aquí está el código de mi bucle:
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. ?>