You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在WordPress代码片段中显示发布日期而非最后修改日期

解决WordPress子主题修改文章日期显示的问题

我来帮你搞定这个问题!你遇到的核心问题是:子主题里复制的template-tags.php没有正确覆盖父主题的函数,这是因为父主题的函数定义用了if ( ! function_exists( 'writings_posted_on' ) )的条件判断——只有当这个函数还没被定义时,父主题才会加载它的版本。而WordPress的加载顺序是先加载子主题的functions.php,但如果你只是把template-tags.php放到子主题的inc文件夹,父主题很可能已经在子主题的这个文件加载前就定义了该函数,导致你的修改完全没生效。

正确解决方案:在子主题functions.php中重写函数

不需要复制整个template-tags.php文件,直接在子主题的functions.php里重新定义writings_posted_on函数,就能完全覆盖父主题的版本。具体代码如下:

if ( ! function_exists( 'writings_posted_on' ) ) :
/**
 * Prints HTML with meta information for the current post-date/time and author.
 */
function writings_posted_on() {
	// 仅保留原始发布日期的time标签,移除修改日期相关逻辑
	$time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time>';

	$time_string = sprintf( $time_string,
		esc_attr( get_the_date( 'c' ) ),
		esc_html( get_the_date() )
	);

	$posted_on = sprintf(
		/* translators: %s: post date. */
		esc_html_x( 'Posted on %s', 'post date', 'writings' ),
		'<a href="' . esc_url( get_permalink() ) . '" rel="bookmark">' . $time_string . '</a>'
	);

	$byline = sprintf(
		/* translators: %s: post author. */
		esc_html_x( 'by %s', 'post author', 'writings' ),
		'<span class="author vcard"><a class="url fn n" href="' . esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ) . '">' . esc_html( get_the_author() ) . '</a></span>'
	);

	echo '<span class="posted-on">' . $posted_on . '</span><span class="byline"> ' . $byline . '</span>'; // WPCS: XSS OK.
}
endif;

代码修改说明:

  1. 删掉了判断发布时间与修改时间是否不同的逻辑,直接固定使用仅包含原始发布日期的$time_string
  2. 移除了所有和get_the_modified_date()相关的代码,确保只输出原始发布日期
  3. 保留了作者信息的输出(如果你不需要可以自行删除,这里按你原需求保留)

操作步骤:

  1. 登录WordPress后台,进入「外观」→「主题文件编辑器」
  2. 在右侧选择你的子主题,找到并打开functions.php文件
  3. 将上面的代码粘贴到文件末尾(注意不要破坏已有的代码结构)
  4. 点击「更新文件」保存修改
  5. 刷新文章页面,就能看到只显示原始发布日期了

这种方法比复制整个文件更安全,父主题更新时也不会影响到子主题的修改。

内容的提问来源于stack exchange,提问作者Grzegorz Chmiel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:09:53