Php 获取post-WordPress的附件URL

Php 获取post-WordPress的附件URL,php,wordpress,attachment,custom-post-type,Php,Wordpress,Attachment,Custom Post Type,我正在尝试创建PDF列表(每月创建的时事通讯)。我创建了一个名为“新闻稿”的自定义帖子类型,并将其限制为仅支持“标题” 然后,我使用高级自定义字段插件将文件上载按钮添加到此帖子类型。因此,每篇文章都有一个标题和一个上传pdf的按钮 然后我编写了下面的函数来输出附件列表 function list_newsletters(){ $args = array( 'post_type' => 'newsletters' ); $loop = new WP_Query( $args

我正在尝试创建PDF列表(每月创建的时事通讯)。我创建了一个名为“新闻稿”的自定义帖子类型,并将其限制为仅支持“标题”

然后,我使用高级自定义字段插件将文件上载按钮添加到此帖子类型。因此,每篇文章都有一个标题和一个上传pdf的按钮

然后我编写了下面的函数来输出附件列表

function list_newsletters(){

    $args = array( 'post_type' => 'newsletters' );
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();
        $permalink = get_permalink();
        $title = get_the_title();
        $id = get_the_ID();
        $attachment = wp_get_attachment_url($id);
        echo '<li><a href="'.$attachment.'">'.$title.'</li>';
    endwhile;

}
功能列表\u通讯(){
$args=array('post_type'=>'newsletters');
$loop=新的WP_查询($args);
而($loop->have_posts()):$loop->the_post();
$permalink=get_permalink();
$title=获取标题();
$id=get_the_id();
$attachment=wp\u get\u attachment\u url($id);
回显“
  • ”.$title.“
  • ”; 结束时; }
    但是wp_get_attachment_url($id)似乎不起作用。我想这是因为我应该提供附件ID,而不是帖子ID。我在网上四处查看,找不到一个明确的方法来查找特定帖子的附件ID

    只是为了澄清,每篇文章将只包含一个附件


    提前感谢您

    此示例摘自Codex页面

    $attachments = get_posts(array( 
        'post_type' => 'attachment',
        'numberposts' => -1,
        'post_status' =>'any',
        'post_parent' => $post->ID
    ));
    
    if ($attachments) {
        foreach ( $attachments as $attachment ) {
            echo apply_filters( 'the_title' , $attachment->post_title );
            the_attachment_link( $attachment->ID , false );
        }
    }
    

    此示例取自Codex页面

    $attachments = get_posts(array( 
        'post_type' => 'attachment',
        'numberposts' => -1,
        'post_status' =>'any',
        'post_parent' => $post->ID
    ));
    
    if ($attachments) {
        foreach ( $attachments as $attachment ) {
            echo apply_filters( 'the_title' , $attachment->post_title );
            the_attachment_link( $attachment->ID , false );
        }
    }