2016-07-31 64 views
0

我想通过查询所有帖子来获得一个acf创建的自定义字段。显示登录用户的自定义字段

我试过如下:

<?php 
if (is_user_logged_in()): 

    global $current_user; 
    $current_user = wp_get_current_user(); 
    $author_query = array('post_status' => array('draft'), 
    'author' => $current_user->ID, 
    'meta_query'    => array(
     array(
      'key'  => 'wpgamail_options', 
      'key'  => 'ganalytics_settings', 
     ), 
    ),); 
    $author_posts = new WP_Query($author_query); 
    while($author_posts->have_posts()) : $author_posts->the_post(); 
    ?> 
    <tr> 
    <td> 
     <?php the_title(); ?> 
    </td> 
    <td> 
     <?php $post_date = get_the_date(); echo $post_date; ?> 
    </td> 
    <td> 
     <?php $field = get_field('wpgamail_options', $author_posts->the_post()->ID, true); echo $field['nextfetchtime']; ?> //here I get the error 
    </td> 
    </tr>  
    <?php   
    endwhile; 

else : 
    echo "You are not logged in. Please log in!"; 
endif; 
?> 

我收到以下错误信息:

注意:试图让非对象的属性在未知行0通话 堆栈:3.9772 231600 1. {main}()/home/ubuntu/workspace/index.php:0 3.9774 232048 2. require('/ home/ubuntu/workspace/wp-blog-header.php')/ home/ubuntu/workspace/index.php:17 4.6285 8745184 3. require_once('/ home/ubuntu/workspace/wp-includes/template-loader.php') /home/ubuntu/workspace/wp-blog-header.php:19 4.6328 8788784 4. include('/ home/ubuntu/workspace/wp-content/themes/twentysixteen/page-create-report.php') /home/ubuntu/workspace/wp-includes/template-loader.php:75 1469999466

任何建议,我所使用[get_field()][1]功能做错了什么?

我很感谢你的回复!

回答

1

您正在使用$author_posts->the_post()->ID而您应该使用get_the_ID()(而不是the_ID()它显示ID)。
固定代码。

<?php 
$field = get_field('wpgamail_options', get_the_ID(), true); 
echo $field['nextfetchtime']; ?> 

此外,您不需要做$current_user = wp_get_current_user();。全局已经包含当前用户对象。

1

$author_posts->the_post()用于迭代循环中的帖子索引,您不能使用它来访问当前帖子。相反,你可以使用get_the_ID

<?php $field = get_field('wpgamail_options', get_the_ID(), true); 
     echo $field['nextfetchtime']; ?> 

我希望这会有所帮助。

+0

这是不正确的。 'the_ID()'显示ID,而在这里它需要被返回。在这种情况下使用正确的函数是'get_the_ID()' –

+0

@IgorYavych很好的评论谢谢!但只是一个评论,你不需要添加像你解决NP难题的答案:D –