2016-07-31 58 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 –

相關問題