2012-03-29 76 views
0

当涉及到图片附件时,wordpress具有非常棒的功能 - 但我找不到任何关于如何获取不是图片附件的文档。wordpress获取不是图片的附件

现在,我写了这个功能:

function ok99_get_post_file_attachment($mime='application/pdf',$limit=-1) { 
    global $post; 
    $attachments = get_children(array('post_parent' => $post->ID, 'post_status' => 'inherit', 'post_type' => 'attachment','post_mime_type' => $mime, 'order' => 'ASC', 'orderby' => 'menu_order ID', 'posts_per_page'=>$limit)); 

    if ($attachments) { 
     echo 'Your Attachments : <br/>'; 
     foreach ($attachments as $att) { 
     //$attachment = array_shift($attachments); // debug to delete 
     echo wp_get_attachment_url($att->ID) . '<br/>'; 
     the_attachment_link($attachment->ID, false);} 
    } 

    return false; 
} 

我的问题是:这是获得所有非图片附件的唯一途径? 有没有办法做到这一点,没有另一个查询?

回答

0

看来,我再一次需要回答自己 - 也因为我没有其他的输入,这个问题

似乎有这样做(指没有其他的办法 - 有很多的方法 - 但并非没有其他直接查询)

+0

我有点晚这个派对,所以也许你的问题已经发生了变化,但是无论如何,看到我的答案是一个一个查询的解决方案!:) – Simon 2014-02-13 13:35:05

0

我通过指定MIME类型,像这样得到的非图像附件:

if ($attachments = get_children(array(
    'post_type' => 'attachment', 
    'post_mime_type' => array('application/doc','application/pdf', 'text/plain'), 
    'numberposts' => 15, 

))); 
foreach ($attachments as $attachment) { 
echo '<a href="' . wp_get_attachment_url($attachment->ID) . '">Download Fact Sheet</a>'; 
echo '</div>'; 
} 
+0

如果文档不是这三种类型之一呢? – durron597 2012-11-30 18:35:03

+0

只需添加到列出MIME类型的数组中 – rezon8dev 2012-12-03 16:59:33

+0

与我在原始问题中写的内容有何不同? – 2014-02-13 15:40:31

1

我使用这种技术用于查询的图像,或没有图像或视频文件,即“否定”MIME类型查询。它返回后对象(类似于将由get_posts()返回数组,并可以很容易地使用vsprintf()进行修改,以排除其他类型的连接介质,或取得完全通用。

function wpse9927425_get_downloads($post_id) { 
    global $wpdb; 

    $sql_query = $wpdb->prepare(
     "SELECT * FROM $wpdb->posts 
     WHERE post_type = %s 
      AND post_parent = %d 
      AND post_mime_type NOT LIKE %s 
      AND post_mime_type NOT LIKE %s 
     ORDER BY menu_order, post_title ASC", 
     'attachment', 
     $post_id, 
     'image/%', 
     'video/%' 
    ); 
    $files = $wpdb->get_results($sql_query); 

    return $files; 
} 
+0

+1谢谢。尽管我的原始问题是为了这样做而不需要额外的查询 - 这种技术确实非常有趣。 – 2014-02-13 15:39:40