在 Astra 主題中顯示“最后更新”日期而非“發(fā)布”日期

Astra 主題 中,默認(rèn)情況下會(huì)顯示文章的 發(fā)布日期,而 最后更新日期 則出現(xiàn)在代碼中,供 SEO Schema 標(biāo)記使用。如果希望在前端展示 最后更新日期 而非發(fā)布日期,添加自定義代碼即可實(shí)現(xiàn)這一功能。

圖片[1]-如何在 Astra 主題中顯示“最后更新”日期而非“發(fā)布”日期

以下是操作步驟。

圖片[2]-如何在 Astra 主題中顯示“最后更新”日期而非“發(fā)布”日期

在文章元數(shù)據(jù)中顯示最后更新日期

圖片[3]-如何在 Astra 主題中顯示“最后更新”日期而非“發(fā)布”日期

標(biāo)記中提供了發(fā)布日期和更新日期。

要僅在文章的元數(shù)據(jù)中顯示 最后更新日期,可以添加以下代碼:

/**
 * 僅在文章元數(shù)據(jù)中顯示最后修改日期。
 *
 * @param String $output 最后修改日期的標(biāo)記。
 * @return void
 */
function your_prefix_post_date( $output ) {
	$output        = '';
	$format        = apply_filters( 'astra_post_date_format', '' );
	$modified_date = esc_html( get_the_modified_date( $format ) );
	$modified_on   = sprintf(
		esc_html( '%s' ),
		$modified_date
	);
	$output       .= '';
	$output       .= ' ' . $modified_on . '';
	$output       .= '';
	return $output;
}
add_filter( 'astra_post_date', 'your_prefix_post_date' );

這段代碼會(huì)隱藏 發(fā)布日期,并僅顯示 最后更新日期。網(wǎng)站訪客將看到最新的修改時(shí)間。

圖片[4]-如何在 Astra 主題中顯示“最后更新”日期而非“發(fā)布”日期

在博客歸檔頁的日期框中顯示“最后更新”日期

如果希望在 博客歸檔頁面 的日期框中顯示 “最后更新” 日期,而非 發(fā)布日期,使用以下代碼:

/**
 * 在文章方框日期格式中僅顯示最后修改日期。
 *
 * @param String $output 最后修改日期的標(biāo)記。
 * @return void
 */
function astra_date_box_time( $output ) {
    $time_string = '<time class="entry-date published" datetime="%1$s"><span class="date-month">%2$s</span> <span class="date-day">%3$s</span> <span class="date-year">%4$s</span></time><time class="updated" datetime="%5$s">%6$s</time>';
    $output = sprintf(
        $time_string,
        esc_attr( get_the_date( 'c' ) ),
        esc_html( get_the_modified_date( 'M' ) ),
        esc_html( get_the_modified_date( 'j' ) ),
        esc_html( get_the_modified_date( 'Y' ) ),
        esc_attr( get_the_modified_date( 'c' ) ),
        esc_html( get_the_modified_date() )
    );
    return $output;
}
add_filter( 'astra_date_box_time_format', 'astra_date_box_time' );

這段代碼確保在 日期框 中顯示 最后更新日期。

圖片[5]-如何在 Astra 主題中顯示“最后更新”日期而非“發(fā)布”日期

重要說明:

  • 若要在 文章頁面日期框 中都顯示 最后更新日期,需要使用兩個(gè)代碼。
  • 將代碼添加到 子主題functions.php 文件 中。

為什么使用子主題?

使用 Astra 子主題 可以讓您在不修改父主題原始代碼的情況下進(jìn)行定制。這樣,即使父主題更新,所有的自定義修改也不會(huì)丟失。

子主題幫助更好地管理代碼,避免與其他插件或主題發(fā)生沖突。自定義代碼與主題核心功能隔離,確保修改的獨(dú)立性和安全性。

總結(jié)

Astra 主題 中,添加自定義代碼后,您可以輕松實(shí)現(xiàn) 在文章和日期框中顯示最后更新日期。這樣,無論是在單個(gè)文章頁還是博客歸檔頁,訪客都能看到最準(zhǔn)確的更新時(shí)間。使用子主題進(jìn)行修改可確保自定義代碼不受父主題更新的影響,幫助高效管理網(wǎng)站。

最新文章


聯(lián)系我們
教程看不懂?聯(lián)系我們?yōu)槟赓M(fèi)解答!免費(fèi)助力個(gè)人,小企站點(diǎn)!
客服微信
客服微信
電話:020-2206-9892
QQ咨詢:1025174874
郵件:info@361sale.com
工作時(shí)間:周一至周五,9:30-18:30,節(jié)假日休息
? 轉(zhuǎn)載聲明
本文作者:賊將鼠膽
THE END
喜歡就支持一下吧
點(diǎn)贊124 分享
評(píng)論 搶沙發(fā)

請(qǐng)登錄后發(fā)表評(píng)論

    暫無評(píng)論內(nèi)容