Avada主题下替换发布日期为修改日期的问题求助
Avada主题替换文章发布日期为修改日期的可行方案
方案一:PHP钩子替换(无需JS,更稳定)
因无法编辑主题,推荐用Code Snippets插件添加自定义代码,避免主题更新丢失:
- 全局替换单文章页的日期显示为修改日期:
add_filter( 'get_the_date', 'replace_post_date_with_modified', 10, 3 ); add_filter( 'the_date', 'replace_post_date_with_modified', 10, 3 ); function replace_post_date_with_modified( $the_date, $d, $post ) { if ( is_single() && $post->post_type === 'post' ) { return get_the_modified_date( $d, $post ); } return $the_date; }
- 针对Avada专属元信息钩子替换(精准匹配主题结构):
add_filter( 'avada_post_meta_date', 'replace_avada_meta_date_with_modified' ); function replace_avada_meta_date_with_modified( $date_html ) { if ( is_single() ) { $modified_date = get_the_modified_date( get_option( 'date_format' ) ); return preg_replace( '/>(.*?)</', '>' . esc_html( $modified_date ) . '<', $date_html ); } return $date_html; }
方案二:修复JS自动触发问题
解决之前JS需鼠标移动才生效的问题,用DOMContentLoaded结合MutationObserver监听动态加载元素:
document.addEventListener('DOMContentLoaded', function() { function replaceDate() { // 替换Avada元信息中的发布日期,需根据实际页面HTML结构调整选择器 const dateElements = document.querySelectorAll('.fusion-post-meta-date .fusion-post-meta-content'); dateElements.forEach(el => { // 优先从页面已渲染的修改日期元素获取内容,或通过WP全局变量获取 const modifiedDate = document.querySelector('.fusion-post-modified-date')?.textContent; if (modifiedDate) { el.textContent = modifiedDate; } }); } // 首次加载直接替换 replaceDate(); // 监听DOM变化,处理Avada动态生成的元素 const observer = new MutationObserver(function(mutations) { mutations.forEach(mutation => { if (mutation.addedNodes.length) replaceDate(); }); }); // 监听元信息父容器,替换为实际页面中的父元素选择器 observer.observe(document.querySelector('.fusion-post-meta'), { childList: true, subtree: true }); });
将代码添加到Avada后台主题选项 > 高级 > 自定义JS中即可。
方案三:Avada主题构建器可视化修改
若你用Avada主题构建器创建了单文章模板:
- 进入Avada > 主题构建器 > 单文章模板
- 删除原有发布日期模块,添加修改日期模块
- 保存模板后,所有单文章页会自动应用修改日期,无需代码
内容的提问来源于stack exchange,提问作者bee
相关产品推荐
相关产品推荐

