簡體   English   中英

顯示已登錄用戶的自定義字段

[英]Display custom field for logged in user

我正在嘗試通過查詢所有帖子來獲取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行的Unknown中獲取非對象的屬性調用堆棧: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]函數做錯了嗎?

感謝您的答復!

$author_posts->the_post()用於在循環中迭代帖子索引,您不能使用它來訪問當前帖子。 相反,您可以使用get_the_ID

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

我希望這將有所幫助。

您正在使用$author_posts->the_post()->ID而應使用get_the_ID() (而不是顯示ID的the_ID() )。
固定代碼。

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

另外,您無需執行$current_user = wp_get_current_user(); $current_user全局已經包含當前用戶對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM