Wordpress 自定义内容包括函数和应用过滤器
我正在使用WP中的一个函数,该函数在帖子内容中搜索任何自定义标记Wordpress 自定义内容包括函数和应用过滤器,wordpress,Wordpress,我正在使用WP中的一个函数,该函数在帖子内容中搜索任何自定义标记,如果找到,则尝试用同名文件替换标记。一切正常,但当我使用apply_filters重新应用内容格式时,WP还添加了一些结束标记,主要是在一些包含的HTML中,这会导致HTML格式错误 关于如何解决这个问题有什么想法吗?我尝试在包含内容之前应用过滤器,但这会使情况变得更糟 请参见下面的功能: //GET CUSTOM CONTENT WITH INSERTED TAGS function extract_custom_value($
,如果找到,则尝试用同名文件替换标记。一切正常,但当我使用apply_filters
重新应用内容格式时,WP还添加了一些结束标记,主要是
在一些包含的HTML中,这会导致HTML格式错误
关于如何解决这个问题有什么想法吗?我尝试在包含内容之前应用过滤器,但这会使情况变得更糟
请参见下面的功能:
//GET CUSTOM CONTENT WITH INSERTED TAGS
function extract_custom_value($string, $start, $end){
//make lower case
$string = strtolower($string);
//count how many tags
$count = substr_count($string, $start);
//create tags array
$custom_tags = array();
if ($count >= 1) {
//set the initial search position to 0
$pos_start = -1;
for ( $i = 0; $i < $count; $i++ ) {
//find custom tags positions
$pos_start = strpos($string, $start, $pos_start + 1);
$pos_end = strpos($string, $end, ($pos_start + strlen($start)));
//set start and end positions of custom tags
$pos1 = $pos_start + strlen($start);
$pos2 = $pos_end - $pos1;
//add to array
$custom_tags[$i] = substr($string, $pos1, $pos2);
}
return $custom_tags;
} else {
return false;
}
}
function get_custom_content(){
//get the content from wordpress
$content = get_the_content();
//find any custom tags
$custom_tags = extract_custom_value($content, '<%', '%>');
//if there is custom tags
if ( $custom_tags ) {
foreach ( $custom_tags as $tag ) {
//make file name from tag
$file = TEMPLATEPATH . '/' . $tag . '.php';
//check if it exists a file with the tag name
if ( is_file($file) ) {
//include the content of the file
ob_start();
include $file;
$file_content = ob_get_contents();
ob_end_clean();
} else {
$file_content = false;
}
//replace the tag with the file contents
$content = str_replace('<%' . $tag . '%>', $file_content, $content );
}
}
//re-apply WP formating to the content
$content = apply_filters('the_content', $content);
//clean up
$content = str_replace(']]>', ']]>', $content);
//show it
print $content;
}
//使用插入的标记获取自定义内容
函数提取\自定义\值($string,$start,$end){
//小写
$string=strtolower($string);
//数一数有多少个标签
$count=substr\u count($string,$start);
//创建标记数组
$custom_tags=array();
如果($count>=1){
//将初始搜索位置设置为0
$pos_start=-1;
对于($i=0;$i<$count;$i++){
//查找自定义标记位置
$pos_start=strpos($string,$start,$pos_start+1);
$pos_end=strpos($string,$end,($pos_start+strlen($start));
//设置自定义标记的开始和结束位置
$pos1=$POSU start+strlen($start);
$pos2=$pos_end-$pos1;
//添加到数组
$custom_tags[$i]=substr($string,$pos1,$pos2);
}
返回$custom_标签;
}否则{
返回false;
}
}
函数get_custom_content(){
//从wordpress获取内容
$content=获取内容();
//查找任何自定义标记
$custom_tags=提取_custom_值($content,”);
//如果有自定义标记
如果($custom_标记){
foreach($custom_标记为$tag){
//从标记生成文件名
$file=TEMPLATEPATH.'/'.$tag..php';
//检查是否存在具有标记名的文件
if(is_文件($file)){
//包括文件的内容
ob_start();
包括$file;
$file_content=ob_get_contents();
ob_end_clean();
}否则{
$file\u content=false;
}
//用文件内容替换标记
$content=str_replace(“”,$file_content,$content);
}
}
//对内容重新应用WP格式化
$content=apply_过滤器('the_content',$content);
//清理
$content=str_replace(']]>',']]',$content);
//表现出来
打印$content;
}
多亏为我指明了使用WP短代码API的正确方向,我已经解决了这个问题,现在有了一个更精简的脚本。如果有人想知道怎么做,请点击此处:
function insert_file($atts){
extract(shortcode_atts(array(
'file' => false
), $atts));
if ( $file == false ){
$file_content = false;
}else{
$file = TEMPLATEPATH . '/' . $file . '.php';
if ( is_file($file) ) {
ob_start();
include $file;
$file_content = ob_get_contents();
ob_end_clean();
} else {
$file_content = false;
}
}
return $file_content;
}
add_shortcode('insert', 'insert_file');
你不能改用Wordpress短代码API吗?谢谢你,理查德。!我来看看。