可恢复的致命错误-无法将类WP_Post的对象转换为字符串

时间:2020-04-03 作者:EdgarAlexPoe

我正在使用ACF并试图根据当前时间显示帖子类型标题。所以我有一个带有start\\u time和end\\u time的post类型条目,我的条件是显示div.onAir,但我得到了这个可恢复的致命错误。这是我的代码:

<?php
    $time_now = date("g:i a");
    $shows = get_posts( array(
      \'post_type\' => \'shows\',
      \'meta_query\' => array(
        array(
          \'key\' => \'start_time\',
          \'compare\' => \'<=\',
          \'value\' => $time_now,
          \'type\' => \'DATETIME\',
        ),
        array(
          \'key\' => \'end_time\',
          \'compare\' => \'<=\',
          \'value\' => $time_now,
          \'type\' => \'DATETIME\',
        )
      ),
    ));
  if( $shows ) {
    foreach( $shows as $show ){ ?>
       <div class="onAir">
         Currently On Air: <?php echo the_title($show); ?>
       </div>
  <?php
  wp_reset_query();
}
} ?>
我觉得很接近,但我以前从未看到过这个错误。任何帮助都将不胜感激!谢谢

UPDATE:

Tom更改了我的代码以使用WP\\U查询。我对ACF和WP中的时间戳匹配有问题。一旦我弄明白了这一点,这段代码将正确地比较我想要看的节目帖子。请注意,我使用date_i18n(\'g:i a\'); 修复了我的本地化时间戳。谢谢汤姆和尤里!

<?php

$time = date_i18n(\'g:i a\');
$shows = get_field(\'station_shows\', false, false);
$query = new WP_Query(array(
  \'post_type\'       => \'shows\',
  \'posts_per_page\'  => 1,
  \'post__in\'            => $shows,
  \'meta_query\' => array(
    \'relation\' => \'AND\',
      array(
          \'key\'     => \'start_time\',
          \'compare\' => \'<=\',
          \'value\'   =>  date_i18n(\'g:i a\', $time),
          \'type\'    => \'TIME\',
      ),
      array(
          \'key\'     => \'end_time\',
          \'compare\' => \'>=\',
          \'value\'   => date_i18n(\'g:i a\', $time),
          \'type\'    => \'TIME\',
      )
  ),
 ) );
 if ( $query->have_posts() ) { while( $query->have_posts() ) { 
 $query-   >the_post();

 echo \'<div class="onAir"><h3>Currently On Air @ \';
 the_title();
 if (get_field(\'dj\', $query->ID)) {
           $dj = get_field(\'dj\');
           echo \' w/ \';
        echo $dj;
    }
     echo \'</h3></div>\';
    } wp_reset_postdata();
  }
 ?>

2 个回复
最合适的回答,由SO网友:Tom J Nowell 整理而成

the_title 不是这样的:

the_title( $before, $after, $echo );
$before 是标题之前的文本,但您没有给它一个字符串/文本,而是给它一个post对象。Post对象不是字符串,PHP不知道该做什么,所以它会停止并打印错误。

对于the_title 要工作,您需要设置当前的postdata。通常,标准循环通过调用the_post 但您已选择使用get_posts 相反

这是一个多么标准的帖子WP_Query post循环应如下所示:

$query = new WP_Query([ ... ]);
if ( $query->have_posts() ) {
    while( $query->have_posts() ) {
        $query->the_post();
        //.... display post here
    }
    wp_reset_postdata();
}

SO网友:Neolot

您错误地调用了WordPress函数。我修复了,请尝试此代码。

<?php
    $time_now = date( "g:i a" );
    $shows    = get_posts( array(
        \'post_type\'  => \'shows\',
        \'meta_query\' => array(
            array(
                \'key\'     => \'start_time\',
                \'compare\' => \'<=\',
                \'value\'   => $time_now,
                \'type\'    => \'DATETIME\',
            ),
            array(
                \'key\'     => \'end_time\',
                \'compare\' => \'<=\',
                \'value\'   => $time_now,
                \'type\'    => \'DATETIME\',
            )
        ),
    ) );

    if ( $shows ) :
        foreach ( $shows as $show ) :
    ?>

    <div class="onAir">
        Currently On Air: <?php echo get_the_title($show->ID); ?>
    </div>

    <?php
        endforeach;
        wp_reset_postdata();
    endif;
?>

相关推荐