首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在Wordpress页面中显示所需的日期?

如何在Wordpress页面中显示所需的日期?
EN

Stack Overflow用户
提问于 2018-09-20 05:33:23
回答 1查看 574关注 0票数 1

我正在尝试找到一种方法来选择日期并将其显示在Wordpress页面中。简单地说,这就是它可能的工作方式:

  1. 一个或多个页面通过PHP insert
  2. 输入短码或PHP码操作员/网站管理员从日历或下拉菜单中选择日期
  3. 此日期(已格式化)显示在具有短码的网页中/PHP码

我不想要自动或当前日期,我可以用这个plugin做。任何人都可以通过插件或PHP代码片段来帮助实现这一点吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-20 07:34:06

Settings->Reading中显示自定义日期字段

function wpse_52414489_custom_date() {
// Add the section to reading settings so we can add our
// fields to it
 add_settings_section(
    'eg_custom_settings',
    'My custom settings',
    '',
    'reading'
 );

// Add the field with the names and function to use for our new
// settings, put it in our new section
 add_settings_field(
    'eg_custom_date',
    'My custom date',
    'eg_custom_date_callback',
    'reading',
    'eg_custom_settings'
 );

// Register our setting so that $_POST handling is done for us and
// our callback function just has to echo the <input>
 register_setting( 'reading', 'eg_custom_date' );
} 

function eg_custom_date_callback() {
 echo '<input name="eg_custom_date" id="eg_custom_date" type="date" value="' . get_option( 'eg_custom_date' ) . '" class="code" /> Explanation text';
}

add_action( 'admin_init', 'wpse_52414489_custom_date' );

添加您的快捷方式(编辑:自定义日期格式):

add_filter( 'init', function() {

 add_shortcode( 'my-custom-date', function() {

    return date( 'd/m/Y', strtotime( get_option( 'eg_custom_date' ) ) );

 });

});

用法:

[my-custom-date]

输出:

2018-09-18 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52414489

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档