在wordpress上列出附件

时间:2011-06-16 11:22:52

标签: wordpress attachment

我有这段代码:

 <?php
                $valid_ext = array("pdf", "doc");
                $args = array(
                    'post_type' => 'attachment',
                    'post_mime_type' => 'application/pdf',
                    'numberposts' => 40,
                    'post_status' => null,
                    'post_parent' => null, // any parent
                    );
                $attachments = get_posts($args);
                if ($attachments) {
                    foreach ($attachments as $post) {
                $ext = getFileExt(wp_get_attachment_url($post_id, false));
                if(in_array($ext, $valid_ext)) {
                 ?>
                <li> 
                    <div class="entry-date">
                        <abbr class="published" title="<?php the_time('Y-m-d\TH:i:sO')?>">
                <?php unset($previousday); printf( __( '%1$s &#8211; %2$s', 'sandbox' ), the_date( '', '', '', false ), get_the_time() )?>
                        </abbr>
                    </div> 
                    <div id="post-<?php the_ID()?>" class="<?php sandbox_post_class()?> " style="padding:0; margin:0;">
                        <p style="font-size:11px; text-transform:uppercase;">
                            <?php setup_postdata($post);the_attachment_link($post_id, true);?>
                        </p>
                    </div> 
                </li>
                <?php
                                }
                    }
                }
            ?>

它为输出提供了日期和文档名称的链接。

<li> 
<div class="entry-date">
    <abbr title="2011-06-15T17:30:29+0200" class="published">
            15 giugno 2011 &ndash; 17:30        
            </abbr>
</div> 
<div style="padding:0; margin:0;" class="hentry p11 attachment inherit author-daniela-santanch category-senza-categoria untagged y2011 m06 d15 h17 " id="post-2158">
    <p style="font-size:11px; text-transform:uppercase;">
        <a title="la_stampa_07_06_2011" href="http://www.mpli.it/wp/wp-content/uploads/2011/06/la_stampa_07_06_2011.pdf">la_stampa_07_06_2011</a>                            </p>
</div> 

我希望将帖子标题直接链接到文档。 有什么帮助吗?

1 个答案:

答案 0 :(得分:1)

试试这个:

<?php
$args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => null ); 
$attachments = get_posts( $args );
if ($attachments) {
    foreach ( $attachments as $post ) {
        setup_postdata($post);
        the_title();
        the_attachment_link($post->ID, false);
        the_excerpt();
    }
}
?>