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.

图片转代码服务由CSDN问答提供 功能建议

我想创建一个在按字母顺序排列的列表中显示3个ACF值的页面。 我有一个自定义的帖子类型=“产品”,并希望显示以下twi字段(两者都是ACF文本字段)1。vendor_or_brand_name和2. product_url

我可以输出所有帖子标题 从产品发布类型如下:

  function output_product_list(){
 global $ wpdb; 
 $ custom_post_type ='product';  
 $ results = $ wpdb-&gt; get_results($ wpdb-&gt; prepare(“SELECT ID,post_title FROM {$ wpdb-&gt; posts} WHERE post_type =%s和post_status ='publish'”,$ custom_post_type),  ARRAY_A); 
 if(!$ results)
 return; 
 $ output ='&lt; ul id =“products”&gt;'; 
 foreach($ results as $ index =&gt; $ post){\  n $ output。='&lt; li id =“'。$ post ['ID']。'”&gt;'  。  $ post ['post_title']。  '&lt; / li&gt;'; 
} 
 $ output。='&lt; / ul&gt;';  //选择元素的结尾
返回$ output; 
} 
   
 
 

但是当我尝试将'post_title'修改为任何一个ACF字段值时,我得到 没有输出。 请指教。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

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