dongzhan8001 2017-11-14 17:09 采纳率: 0%
浏览 6
已采纳

WP显示页面上所有帖子的ACF值

I would like to create a page that displays 3 ACF Values in an alphabetized list. I have a custom post type ="product" and would like to display the following twi fields (Both are ACF text fields) 1. vendor_or_brand_name and 2. product_url

I can output all post titles from the product post type like this:

function output_product_list() {
    global $wpdb;
    $custom_post_type = 'product'; 
    $results = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_title FROM {$wpdb->posts} WHERE post_type = %s and post_status = 'publish'", $custom_post_type ), ARRAY_A );
    if ( ! $results )
            return;
            $output = '<ul id="products">';
            foreach( $results as $index => $post ) {
                    $output .= '<li id="' . $post['ID'] . '">' . $post['post_title'] . '</li>';
                }
            $output .= '</ul>'; // end of select element
            return $output;
}

But when I try and modify 'post_title' to either of the ACF field values I get no output. Please advise.

  • 写回答

2条回答 默认 最新

  • dtc88867 2017-11-14 17:16
    关注

    That's because you're querying specifics in the DB... the fields don't exist there. ACF has existing functions to get field values e.g. get_field()

    So in your case:

    function output_product_list() {
        global $wpdb;
        $custom_post_type = 'product'; 
        $results = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_title FROM {$wpdb->posts} WHERE post_type = %s and post_status = 'publish'", $custom_post_type ), ARRAY_A );
        if ( ! $results )
                return;
                $output = '<ul id="products">';
                foreach( $results as $index => $post ) {
                        $output .= '<li id="' . $post['ID'] . '">' . get_field('ACF_FIELD_NAME',$post['ID']) . '</li>';
                    }
                $output .= '</ul>'; // end of select element
                return $output;
    }
    

    Be sure to change the ACF_FIELD_NAME to what ever you named the ACF field in the WP Admin

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化