【发布时间】:2016-04-03 15:49:13
【问题描述】:
我想使用外部 HTML 作为我的 Wordpress 网站某些页面的内容。
即,目前我将 html 直接放入 wordpress 中的页面。但是,我不喜欢这样,因为我喜欢使用具有语法高亮、搜索/替换等功能的编辑器。 我想知道是否有一种方法可以通过 functions.php 文件从外部 HTML 文件中调用内容以插入到页面内容中。
(我不想使用 javascript/jquery。我已经可以有效地工作,但我想知道是否有办法通过 php。)
更新 - 答案
在遵循两个链接(@zipkundan、@pierre)的说明后,这是我拼凑的最终代码,就像一个魅力一样:
// add the filter call which will change the content
add_filter( 'the_content', 'insert_html' );
// the callback function - it gets the variable $content which holds default content
function insert_html($content) {
// I needed the content filtered by page - only for the 'help' page
if( is_page( 'help' )) {
// this line gets the html from the file - stores into myHtml var
$myHtml = file_get_contents(url_to_file);
return $myHtml;
}
// if the page is not 'help', we need to return the content, otherwise page will be empty
return $content;
}
【问题讨论】:
-
您可以在模板中按功能包含文件
include('path/to/file') -
@Pierre,谢谢 - 这是一个很好的链接,显示了 php 函数,但它没有回答问题。我正在寻找一种使用 functions.php 文件插入它的方法。我不想为此页面制作单独的模板。我需要知道是否有某种类型的内容挂钩/过滤器可以让我插入此内容。 (因此,这不是重复的)