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条)

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致