wp_get_attachment_url生成不同的链接

时间:2018-07-10 10:29:03

标签: wordpress

我编写此代码是为了获取帖子的所有附件:

$args = array(
    'order'          => 'ASC',
    'post_type'      => 'attachment',
    'post_parent'    => $pid,
    'exclude'    => $exclude,
    'numberposts'    => -1,
    'post_status'    => null,
);
$attachments = get_posts($args);


foreach($attachments as $at)
{
    ?>
    <li> <a href="<?php echo wp_get_attachment_url($at->ID); ?>"><?php echo $at->post_title; ?></a>
    </li>
<?php } ?>

对于.zip.xlsx文件类型,它会生成如下网址:

http://localhost/mywp/wp-contents/uploads/test.zip(可以)

但是对于.docx文件类型,它会生成如下所示的网址:

http://localhost/mywp/?attachment_id=710 (此链接对我不起作用(下载)。)

出什么问题了?

2 个答案:

答案 0 :(得分:1)

我的问题是Ajax上传! wp_handle_upload有错误。 (MIME类型错误)和wp_insert_attachment插入空记录。并导致创建错误的链接。 谢谢。

答案 1 :(得分:0)

您尝试过get_attachment_link()吗?这是示例代码

$attachment_id = 1; // ID of attachment
$attachment_page = get_attachment_link( $attachment_id ); 
?>
<a href="<?php echo $attachment_page; ?>"><?php echo get_the_title( $attachment_id ); ?></a>