如何在 WordPress 的帖子循环中使用 get_field()?

How to get_field() in posts loop in WordPress?

我正在尝试为短代码创建一个函数以循环遍历 'articles' 自定义 post 类型。这本质上是用户上传的可下载文件,是自定义 post 类型,在 post 编辑器中没有内容 field/editor 框。此自定义 post 类型还附加了一个 'download_link' ACF 字段,这是下载文件的位置。这是供稿中每个 post 的 href 属性。但是,该字段不会在下面的函数中生成任何输出。但是标题正在填充。我知道所有文章 post 都为这个特定字段提供了一个值。我错过了什么吗?谢谢。

// CREATE A FEED OF ARTICLES WITH LINKS
function articles_download_feed(){
    $articles_query = new WP_Query(array('post_type' => 'article', 
        'post_status' => 'any'));
        if( $articles_query->have_posts() ){
            $output = '<ul>';
            while( $articles_query->have_posts() ){
                $articles_query->the_post();
                $articleID = get_the_ID();
                $title = get_the_title($articleID);
                if(get_field('download_link', $articleID)){
                    $download = get_field('download_link', $articleID, true);
                } else {
                    $download = 'ABC';
                }
                $output .= '<li><a target="_blank" href="'.$download.'">'.$title.'</a></li>';
        }
    $output .= '</ul>';
    wp_reset_postdata();
    return $output;
    } 
} add_shortcode('articles_feed', 'articles_download_feed');

我自己设法解决了这个问题。

我通过 add_post_meta() WP 函数为 'article' 类型的帖子设置了 'dile_dl_link' 自定义字段,而不是通过高级自定义字段(下面是用于创建新文章):

// CREATE NEW ARTICLE LINK FROM GRAVITY FORM SUBMISSION
function article_upload_createlink($entry, $form){
    // Fields
    $post_title = ucwords($entry[8]);
    $post_status = 'publish';
    $post_excerpt = $entry[9];
    $post_type = 'article';
    $dl_link = $entry[3];
    // Post being created
    $new_article = array(
      'post_title' => $post_title,
      'post_status' => $post_status,
      'post_excerpt' => $post_excerpt,
      'post_type' => $post_type, 
    );
    // ID for post
    $theId = wp_insert_post($new_article);
    // Update download_link
    add_post_meta($theId, 'file_dl_link', $dl_link);
} add_action( 'gform_after_submission_7', 'article_upload_createlink', 10, 2 );

问题中出现的相关功能已更改如下:

// CREATE A FEED OF ARTICLES WITH LINKS
function articles_download_feed(){
    $articles_query = new WP_Query(array('post_type' => 'article', 'post_status' => 'any'));
    if( $articles_query->have_posts() ){
        $output = '';
        while( $articles_query->have_posts() ){
            $articles_query->the_post();
            $articleID = get_the_ID();
            $title = get_the_title();
            $excerpt = get_the_excerpt();
            $get_dllink = get_post_meta($articleID, 'file_dl_link', true);
            $output .= '<div class="wpb_wrapper">
                    <div class="vc_empty_space" style="height: 24px">
                        <span class="vc_empty_space_inner"></span>
                    </div>
                    <div class="wpb_text_column ">
                        <div class="wpb_wrapper">
                            <h4>'.$title.'</h4>
                            <p>'.$excerpt.'</p></div>
                        </div>
                        <div class="w-btn-wrapper align_left">
                            <a target="_blank" class="w-btn style_raised color_primary icon_none" href="'.$get_dllink.'"><span class="w-btn-label">DOWNLOAD</span><span class="ripple-container"></span></a>
                        </div>
                    </div>
                    <div class="w-separator type_default size_medium thick_1 style_solid color_border cont_none">
                        <span class="w-separator-h"></span>
                    </div>';
        }
        wp_reset_postdata();
        return $output;
    }
}
add_shortcode('articles_feed', 'articles_download_feed');