函数假定wpautop
过滤器打开the_content
位于优先级10,WP将其添加到该优先级。如果出于任何原因,它已被移动到另一个优先级,则应将下面的筛选器调整为在wpautop
.
函数过滤器the_content
如果我们在前端,并且我们在一个单一的帖子/页面上(请随意调整,以便它适用于您实际需要的地方),那么它会检查此特定帖子是否有报价自定义字段。
如果满足以上所有条件,则将内容拆分为两部分,并替换第一次出现的</p>
从下半场开始</p>
+ $quote
. 你也会注意到我从$split_point
如果$content
长度大于8。这样做是为了包括实际拆分发生在结尾内的情况</p>
.
add_filter(\'the_content\', \'split_by_quote\', 11);
function split_by_quote($content) {
global $post;
$custom_meta_key = \'quote\';
$tag = \'blockquote\';
if ((!is_admin()) && is_singular(array(\'post\', \'page\')) // adjust to your needs
&& $quote = get_post_meta($post->ID, $custom_meta_key, true)) {
$split_point = intval(strlen($content)/2);
$split_point = strlen($content > 8) ? $split_point - 4 : $split_point;
$first_half = substr($content, 0 , $split_point);
$second_half = substr($content, $split_point);
$second_half = preg_replace(
\'|<\\/p>|\',
"</p><{$tag}>".$quote."</{$tag}>",
$second_half,
1
);
$content = $first_half.$second_half;
}
return $content;
}
这个应该放进去
functions.php
你的主题,最好是儿童主题。如果您只想在特定的帖子或页面中使用它,只需添加
is_page({id_here})
或
is_single({id_here})
根据滤清器的状况,拆下
is_singular()
.
针对没有</p>
在下半年,它不会输出报价,但这基本上意味着wpautop
未对该内容运行筛选器。因此添加:
remove_filter(\'the_content\', \'wpautop\');
add_filter(\'the_content\', \'wpautop\');
在你的
functions.php
应使其工作(输出报价)。
如果你的所有内容都在一个大的</p>
, 报价将在内容之后输出。
另一件需要检查的事情是,您是否在与$tag
(您不希望在另一个blockquote中包含blockquote)。上述函数不进行该检查。