更改WordPress自定义字段的日期格式

huangapple go评论61阅读模式
英文:

Change custom field date formating WordPress

问题

我已经为我的自定义文章类型“Event”创建了一个自定义字段“事件日期”。我尝试在前端显示日期,并且想要更改日期显示的格式,但似乎无法工作。这是我尝试过的代码:

<div class="single-post-event-meta">
    <p><?php 
        $event_date = get_post_meta(get_the_ID(), '_event_post_date_value_key');
        echo $event_date[0]->format('M');
    ?></p>
</div>

谢谢帮助!

英文:

I have made a custom field 'Event date' for my custom post type 'Event'. I'm trying to display the date on the front-end and I want to change the format of how the date is displayed, but it doesn't seem to work. Here's what I tried:


 &lt;div class=&quot;single-post-event-meta&quot;&gt;
                &lt;p&gt;&lt;?php 

                    $event_date = get_post_meta(get_the_ID(), &#39;_event_post_date_value_key&#39;);
                    echo $event_date[0]-&gt;format(&#39;M&#39;);
                
                ?&gt;&lt;/p&gt;
            &lt;/div&gt;

Thanks for the help!

答案1

得分: 0

看,我回答了自己的问题,哇喔喔!

&lt;p class=&quot;single-post-event-meta-date&quot;&gt;&lt;?php 
   echo &#39;日期:&#39;;
   echo (new DateTime(get_post_meta($post&gt;ID,&#39;_event_post_date_value_key&#39;,true)))-&gt;format(&#39;d M Y&#39;);
 ?&gt;&lt;/p&gt;
英文:

Look I answered my own question WoohoooO!


&lt;p class=&quot;single-post-event-meta-date&quot;&gt;&lt;?php 
   echo &#39;Date: &#39;;
   echo (new DateTime(get_post_meta($post&gt;ID,&#39;_event_post_date_value_key&#39;,true)))-&gt;format(&#39;d M Y&#39;);
 ?&gt;&lt;/p&gt;

huangapple
  • 本文由 发表于 2023年7月11日 01:04:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76655894.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定