【问题标题】:How do I format the post date in Wordpress?如何在 Wordpress 中格式化发布日期?
【发布时间】:2013-08-11 18:52:37
【问题描述】:

我有一个侧边栏,我想在其中显示最近的帖子。现在它显示标题、日期和摘录。日期显示我想摆脱的时间。我用这个来显示日期:$recent["post_date"]

 <?php
$args = array( 'numberposts' => '3' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
    echo '<li id="sidebar_text"><b>'.$recent["post_title"].'</b></li><li style="font-size:12px">'.$recent["post_date"].'</li><li><i style="font-size:15px">'.$recent["post_excerpt"].'</i><a href="'.get_permalink($recent["ID"]).'"> Read   More</a></li>';
     }
 ?>

它显示的日期是这样的:2013-08-11 18:29:04,我想要这样的 8-11-2013 并且没有时间。提前致谢。

【问题讨论】:

    标签: php wordpress sidebar


    【解决方案1】:
    date('n-j-Y', strtotime($recent['post_date']));
    

    这将按照您想要的方式对其进行格式化。只需用它替换循环中的$recent['post_date']

    【讨论】:

    • Syfaro 的回答会得到想要的结果,但如果您希望您的网站以同样的方式始终如一地显示日期,您最好更改日期在“设置”> 中的显示格式General' 部分(如果这是您分发给其他人使用的插件的一部分,则尤其重要,因为他们希望看到他们的本地日期格式)。然后,您可以使用默认情况下不返回时间的 get_the_date 函数。记录在这里:codesynthesis.co.uk/code-snippets/…
    • 使用 get_option( 'date_format' ) 获取设置 > 常规 > 日期格式中定义的默认日期格式
    【解决方案2】:

    虽然 Syfaro 的回答是正确的,但最佳做法是为此使用 WordPress 自己的功能。

    get_the_date

    这默认为 WordPress 管理设置(设置 -> 常规)中设置的格式,因此为将来的编辑提供了更易于访问的解决方案 - 如果您在多个站点中滚动代码特别有用,或者更重要的是,如果您公开发布它.

    另外,别忘了escape output - 查看esc_htmlesc_html_e

    【讨论】:

      【解决方案3】:
      date_i18n('l d/m/Y \à\s g:i', strtotime($item['time']))
      

      【讨论】:

        【解决方案4】:

        $recent["post_date"] 替换为mysql2date('n-j-Y', $recent['post_date'])

        【讨论】:

          猜你喜欢
          • 2020-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-10-20
          • 2012-04-23
          • 1970-01-01
          相关资源
          最近更新 更多