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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?