Wordpress Basic循环问题WP_Query

时间:2016-03-14 13:34:31

标签: php wordpress advanced-custom-fields

我正在开发一个wordpress主题,并且在使用WP_Query获取自定义帖子类型之后遇到了以下问题我的页面高级自定义字段不可用,如下所示:

index.php摘录

    //passing in the post id of page with advanced custom fields called 'home'
    $indexPosts = new WP_Query("post_type" => "page", $post_id = $post->ID); 

    while($indexPosts->have_posts()){
       $indexPosts->the_post();

       //do stuff with home pages advanced field data, all fields available and working
       get_field('somecustomfield_from_home_page'); //NON empty

       //get custom post
       $customPost = new WP_Query("post_type" => "mycustompost");

       while($customPost->have_posts()){
           $custom_post->the_post();

           //do stuff with custom post data, all is working

       }
       wp_reset_postdata();        

       //trying to do stuff with home pages acf data, all fields empty
        get_field('somecustomfield_from_home_page'); //empty
    }
    wp_reset_postdata();

1 个答案:

答案 0 :(得分:1)

我不确定您为什么要使用自定义查询来输出主页发布数据。这应该在默认查询下可用。我高度建议您为主页使用 front-page.php 页面模板(recommended by the docs)。如果您这样做,您可以将所有内容简化为以下内容:

coverImageInit