为此,我修改了一篇旧博客文章中的一些代码,并允许将查询字符串附加到文件中。
原始贷方转至amberpanther.com, 结果他们做了一个plug-in 我也不想这样。
//create the shortcode [include] that accepts a filepath and query string
//this function was modified from a post on www.amberpanther.com you can find it at the link below:
//http://www.amberpanther.com/knowledge-base/using-the-wordpress-shortcode-api-to-include-an-external-file-in-the-post-content/
//BEGIN amberpanther.com code
function include_file($atts) {
//if filepath was specified
extract(
shortcode_atts(
array(
\'filepath\' => \'NULL\'
), $atts
)
);
//BEGIN modified portion of code to accept query strings
//check for query string of variables after file path
if(strpos($filepath,"?")) {
$query_string_pos = strpos($filepath,"?");
//create global variable for query string so we can access it in our included files if we need it
//also parse it out from the clean file name which we will store in a new variable for including
global $query_string;
$query_string = substr($filepath,$query_string_pos + 1);
$clean_file_path = substr($filepath,0,$query_string_pos);
//if there isn\'t a query string
} else {
$clean_file_path = $filepath;
}
//END modified portion of code
//check if the filepath was specified and if the file exists
if ($filepath != \'NULL\' && file_exists(get_stylesheet_directory_uri() . "/" . $clean_file_path)){
//turn on output buffering to capture script output
ob_start();
//include the specified file
include(TEMPLATEPATH.$clean_file_path);
//assign the file output to $content variable and clean buffer
$content = ob_get_clean();
//return the $content
//return is important for the output to appear at the correct position
//in the content
return $content;
}
}
//register the Shortcode handler
add_shortcode(\'include\', \'include_file\');
//END amberpanther.com code
//shortcode with sample query string:
//[include filepath="/get-posts.php?format=grid&taxonomy=testing&term=stuff&posttype=work"]
我将我的设置为从样式表uri中提取(因此它可以处理子主题等),但您可以轻松地调整代码以从任何地方提取(包括插件目录),或者将其全部删除,在包含文件时只使用完整路径。您甚至可以添加一个开头带有触发器字符的条件,告诉它根据文件名的第一个字符是什么来使用特定的路径,例如使用“#”作为模板目录等。
我用它来拉入一个名为get posts的文件。php,它位于我的模板目录中,并根据查询字符串中提供的一系列参数格式化各种帖子的输出。它使我不需要特殊的模板,因为我可以包含文件,以一种格式发送它,它将输出带有我在get Post中指定的标记的帖子。php文件。
它还允许客户端以特定格式将自定义帖子类型拉入实际的博客帖子中,这非常方便。
如果您需要任何澄清,请告诉我。