【问题标题】:Wordpress: store year and month in variableWordpress:将年份和月份存储在变量中
【发布时间】:2013-04-24 17:38:37
【问题描述】:

在 Wordpress 网站的特定存档页面上,我有网址:

http://fruitpage/2013/04/

年份和月份数字告诉 Wordpress 要显示哪些帖子(2013 年 4 月的帖子)。有没有办法使用 Wordpress 功能将 2013 存储在一个变量中,将 04 存储在另一个变量中?

目标是能够回显url中的年份和月份,例如:

echo $urlYear; //echoes out 2013 that is found in the url
echo $urlMonth; //echoes out 04 that is found in the url

【问题讨论】:

  • “存储在变量中”在什么情况下?在哪里?
  • 我希望能够回显 $year 和 $month,从而得到 url 中的值。

标签: wordpress variables date


【解决方案1】:

在存档页面中,您只需在帖子循环中调用the_date() 函数

$year = the_date('Y');

$month = the_date('m');

也可以试试get_the_time()

【讨论】:

  • 你确定可以在archive.php页面中使用吗?
  • 它不适用于 the_date,但 get_the_time 成功了!感谢您为我指明正确的方向:)
【解决方案2】:

就用这些

<?php echo get_the_date('M'); ?> for month
<?php echo get_the_date('Y'); ?> for year

【讨论】:

  • 为什么 OP 应该“只使用这个”? 好的答案将始终解释所做的事情以及为什么以这种方式完成,不仅适用于 OP,而且适用于 SO 的未来访问者。
猜你喜欢
  • 2014-08-07
  • 2016-12-23
  • 2012-12-07
  • 2011-06-27
  • 1970-01-01
  • 2023-04-11
  • 2017-03-25
  • 1970-01-01
  • 2012-06-26
相关资源
最近更新 更多