2017-01-10 96 views
2

这是一个Word Press/PHP的问题(非常初学者,我猜)。我正在尝试使用以下代码插入指向最新博文的链接,然后是发布日期。WordPress的: - get_the_date()函数返回一个错误的日期

<div class="latest_post"> 
    <ul><li><span class="recent_blog">LATEST POST</span><?php 
    $args = array(
     'numberposts' => 1, 
     'category' => 71, 
     'post_status' => 'publish', 
    ); 
    $recent_posts = wp_get_recent_posts($args); 
    foreach($recent_posts as $recent) { 
     echo '<a href="' . get_permalink($recent["ID"]) . '"> <strong>' . $recent["post_title"].'</strong></a>'; 
    } 
    wp_reset_query(); 
    ?> (<?php echo get_the_date('Y/m/d'); ?>)</li></ul> 
</div><!-- .latest_post --> 

然而,​​返回一个错误的日期“(2015年4月23日)”,我不知道它是从哪里来的。它应该是(2017/01/02)。任何人都可以帮我找出它出错的地方吗?或者,还有其他方法来获取正确的日期?

预先感谢您!

+0

基础上,参考: - HTTPS:/ /developer.wordpress.org/reference/functions/get_the_date/(检索邮件的撰写日期。)。我认为它正在返回一个发布日期。所以如果你想正确的日期使用'<?php回声日期('Y/m/d');?>' –

+1

谢谢,我已经试过'<?php回声日期('Y/m/d');?>'但是这返回了今天的日期,而不是2017/01/02。 – Joey

+1

我想要发布最新帖子的日期(即2017/01/02)。而且,我希望日期在每次新帖子上线时自动更新。 – Joey

回答

4

实际上是基于参考: - https://developer.wordpress.org/reference/functions/get_the_date/

(据检索上的帖子撰写日期。)

因此,要么提供一个帖子ID为这个函数来获得具体的发布日期

或者

如果你想当前日期,那么你可以使用: -

<?php echo date('Y/m/d');?> 

我认为你必须做如下图所示: -

foreach($recent_posts as $recent) { 
    echo '<a href="' . get_permalink($recent["ID"]) . '"> <strong>' . $recent["post_title"].'</strong></a>'; 
    echo get_the_date('Y/m/d',$recent["ID"]); 
} 
+0

我认为这是完成(对不起,我在这里很新)! – Joey

+0

@Joey很高兴帮助你。欢呼:) :) –

+0

@Joey关于为什么您提到的“错误日期”(2015/04/23)显示的某些信息而非您的预期日期(2017/01/02):** get_the_date * *行为是为了_always_返回一个日期,所以如果一个帖子ID没有被赋予一个** get_the_date **调用,并且你不在可以获取帖子的上下文中(即is_date()),那么作为最后的手段* * get_the_date **将使用'wp_posts'表(在Wordpress数据库中)的'post_date'字段作为页面本身(请注意,Wordpress Pages也存储在'wp_posts'表中,但是'post_type'是'page' )。希望这是有用的。 – John

1

您必须像上面一样使用它。

<?php echo get_the_date($format, $post_id); ?> 

$format
(字符串)(可选)PHP日期格式。 默认值:date_format选项(在设置“日期格式”>常规面板)

$post_id
(整数)(可选)后的ID,你想取。默认情况下获取当前帖子。 默认:空

+0

谢谢,添加帖子ID意味着日期不能在下一篇文章上线时自动更新? – Joey

+0

我建议你测试一下... –

1

看起来,基于文档时(https://codex.wordpress.org/Function_Reference/get_the_date

“检索当前 $ post的日期已写入”,以便日期:(2015/04/23)可能引用您正在创建该帖子链接的活动页面。

如果你能得到的帖子ID(新职位),你就可以得到正确的时间: - get_the_date($format, $post_id)

+0

谢谢!这个回声get_the_date('Y/m/d',$ recent [“ID”]);完美工作,获取发布日期。感谢堆! – Joey

+0

不用担心,很高兴它工作:) – Boz