I wish to retrieve the different PDF and ZIP files attached to a single post and display them in a specific section, outside the content, using the WP function get_attached_media()
. While my code works as it should for media files freshly uploaded, I cannot manage to get it working if a file has already been uploaded and used in a different post.
For example, suppose post ID-1 embeds file-1.pdf in the content. Then, if I create post ID-2 and insert again file-1.pdf into the content, get_attached_media( 'application/pdf', $post->ID )
will return an empty array ?!
Am I missing something / should I flush anything (if so, how...) ?
Here is the code of my loop:
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. ?>
I wish to retrieve the different PDF and ZIP files attached to a single post and display them in a specific section, outside the content, using the WP function get_attached_media()
. While my code works as it should for media files freshly uploaded, I cannot manage to get it working if a file has already been uploaded and used in a different post.
For example, suppose post ID-1 embeds file-1.pdf in the content. Then, if I create post ID-2 and insert again file-1.pdf into the content, get_attached_media( 'application/pdf', $post->ID )
will return an empty array ?!
Am I missing something / should I flush anything (if so, how...) ?
Here is the code of my loop:
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. ?>
Share
Improve this question
asked Jun 15, 2015 at 16:45
floflo
1511 silver badge2 bronze badges
3
- 4 an attachment can only be attached to one post. inserting media into a post's content doesn't attach it to the post, only uploading while editing a post will attach it. – Milo Commented Jun 15, 2015 at 17:52
- Thanks @Milo. So the only way to achieve what I need in WP would be to develop a Regex to extract pdf and zip links present in the content of a post ...? – flo Commented Jun 15, 2015 at 17:57
- @milo Your comment should be the accepted answer – Mike Pollitt Commented Oct 9, 2017 at 0:20
1 Answer
Reset to default 1I ran into this problem and ended up creating a basic function to extract the "attached" media by URL from the body of the post (in my case a post of type document, but it should work with any kind of post):
function get_first_link_url($post_id) {
$content = get_post_field('post_content', $post_id);
if ($content == null) {
return null;
}
$dom = new DOMDocument();
$dom->loadHTML($content);
$links = $dom->getElementsByTagName("a");
if (count($links) == 0) {
return null;
}
$url = $links[0]->getAttribute('href');
return $url;
}
This only returns the URL (href) of the first tag in the post, but it could readily be modified to return all of them as an array.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742361817a4429538.html
评论列表(0条)