在之前插入内容<;p>;PHP中的标记
虽然这是面向Wordpress的,但我认为PHP的基础更像是一个Stackoverflow问题: 我想在帖子的第一个在之前插入内容<;p>;PHP中的标记,php,wordpress,loops,for-loop,foreach,Php,Wordpress,Loops,For Loop,Foreach,虽然这是面向Wordpress的,但我认为PHP的基础更像是一个Stackoverflow问题: 我想在帖子的第一个之前添加一个模板部分(类似于include()) 我不能在内容()之前添加模板部分,因为在内容()中有时是,然后: <figure>....</figure> // Want to insert here! <p>.......</p> <p>.......</p> <p>.......</p
之前添加一个模板部分(类似于include()
)
我不能在内容()之前添加模板部分,因为在内容()中有时是
,然后
:
<figure>....</figure>
// Want to insert here!
<p>.......</p>
<p>.......</p>
<p>.......</p>
<figure>....</figure>
<p>.......</p>
<p>.......</p>
。。。。
//想在这里插入!
....
这是我尝试使用的两个代码,但不确定如何在第一个代码之前使用:
<figure>....</figure>
// Want to insert here!
<p>.......</p>
<p>.......</p>
<p>.......</p>
<figure>....</figure>
<p>.......</p>
<p>.......</p>
方法一:
$after = 1;
$content = apply_filters('the_content', $post->post_content);
if(substr_count($content, '<p>') > $after) {
$contents = explode("</p>", $content); $p_count = 0;
foreach($contents as $content){
echo $content; if($p_count == $after){
get_template_part('path/to/part');
} $p_count++;
}
}
$paragraphAfter[1] = get_template_part('path/to/part');
$content = apply_filters( 'the_content', get_the_content() );
$content = explode("</p>", $content);
$count = count($content);
for ($i = 0; $i < $count; $i++ ) {
if ( array_key_exists($i, $paragraphAfter) ) {
echo $paragraphAfter[$i];
}
echo $content[$i] . "</p>";
}
$after=1;
$content=apply_filters('the_content',$post->post_content);
if(substr_count($content,)>$after){
$contents=explode(“”,$content);$p\u count=0;
foreach($contents作为$content){
echo$content;if($p_count==$after){
获取模板零件('path/to/part');
}$p_count++;
}
}
方法二:
$after = 1;
$content = apply_filters('the_content', $post->post_content);
if(substr_count($content, '<p>') > $after) {
$contents = explode("</p>", $content); $p_count = 0;
foreach($contents as $content){
echo $content; if($p_count == $after){
get_template_part('path/to/part');
} $p_count++;
}
}
$paragraphAfter[1] = get_template_part('path/to/part');
$content = apply_filters( 'the_content', get_the_content() );
$content = explode("</p>", $content);
$count = count($content);
for ($i = 0; $i < $count; $i++ ) {
if ( array_key_exists($i, $paragraphAfter) ) {
echo $paragraphAfter[$i];
}
echo $content[$i] . "</p>";
}
$paragraphAfter[1]=get_template_part('path/to/part');
$content=apply_filters('the_content',get_the_content());
$content=explode(“”,$content);
$count=计数($content);
对于($i=0;$i<$count;$i++){
如果(数组\键\存在($i,$paragraphAfter)){
在[$i]之后回显$paragraphy;
}
echo$content[$i]。“”;
}
最好是更有效的方法,无论是从上面还是你自己:肮脏的黑客可以是:
function add_the_string_filter( $content ) {
$string_to_append = 'your string to add';
$content = preg_replace('/<p>/', $string_to_append . '<p>', $content , 1);
return $content;
}
add_filter( 'the_content', 'add_the_string_filter' );
函数添加字符串过滤器($content){
$string_to_append='your string to add';
$content=preg_replace(“//”,$string_to_append.”,$content,1);
返回$content;
}
添加过滤器(“内容”、“添加字符串过滤器”);
preg\u replace
的最后一个参数是限制,它告诉我们只替换第一次出现的代码。所以这是可行的,但我很好奇为什么这是一个“肮脏的黑客”而不是普通代码?我不确定,也许有些WordPress专家会有更好的方法来做。