【问题标题】:Get link or path of attached file of post in Wordpress在 Wordpress 中获取帖子附件的链接或路径
【发布时间】:2020-08-30 13:43:49
【问题描述】:

我有上传文件的帖子。我想做一个可下载的链接。所以我使用<?php echo get_attachment_link(); ?>。但我无法获得文件的完整路径。

这是我的代码

<?php
 $reports = array(
 'post_type' => 'post' , 
'posts_per_page' => 3,
'category_name' => 'reports'); 
                              
    $q_reports = new WP_Query($reports);
                    
        if($q_reports->have_posts()){
        while($q_reports->have_posts()){ 
        $q_reports->the_post();?> 

<li><a href="<?php echo get_attachment_link(); ?>" ><?php echo the_title(); ?></a>
Published: <?php the_date(); ?> 

【问题讨论】:

    标签: php wordpress wordpress-theming custom-wordpress-pages


    【解决方案1】:

    您可以使用wp_get_attachment_url() 获取文件的完整路径。

    查看https://developer.wordpress.org/reference/functions/wp_get_attachment_url/

    【讨论】:

    • wp_get_attachment_url 需要附件 ID,但我不知道如何获取。我在帖子中只附加了一个文件。你能帮我从上面的代码中得到它吗?我是 WP 的新手和菜鸟。
    • 欢迎来到 Stack Overflow。不鼓励仅链接答案,因为链接可能会随着时间的推移而中断或更改。为了使您的答案对 OP 更有用,请对其进行编辑以包含链接中的重要信息以及他们如何使用它来解决问题的说明。
    • @NirdoshShrestha 如果您不提供 ID,此函数将采用默认全局 $post。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-09
    相关资源
    最近更新 更多