我正在尝试将内联样式添加到使用the_content();
我尝试过字符串替换,请参见question 我之前做过。但它不会,因为\\u内容echo是它,并且不会返回它。如果我使用返回内容get_the_content();
, 它不输出段落标记。
有人能帮我吗?
我正在尝试将内联样式添加到使用the_content();
我尝试过字符串替换,请参见question 我之前做过。但它不会,因为\\u内容echo是它,并且不会返回它。如果我使用返回内容get_the_content();
, 它不输出段落标记。
有人能帮我吗?
提出此解决方案只是为了将其应用于特定的内容函数。
在我的问题中,我没有解释我只需要处理特定的the\\u内容,相反,我认为上述解决方案是一个全球性的解决方案,从这个角度来看,两者都是很好的解决方案。
<?php
$phrase = get_the_content();
// This is where wordpress filters the content text and adds paragraphs
$phrase = apply_filters(\'the_content\', $phrase);
$replace = \'<p style="text-align: left; font-family: Georgia, Times, serif; font-size: 14px; line-height: 22px; color: #1b3d52; font-weight: normal; margin: 15px 0px; font-style: italic;">\';
echo str_replace(\'<p>\', $replace, $phrase);
?>
您可以在自定义“the\\u content”函数中使用自定义字符串替换。
function custom_the_content($more_link_text = null, $stripteaser = false) {
$content = get_the_content($more_link_text, $stripteaser);
$content = apply_filters(\'the_content\', $content);
$content = str_replace(\']]>\', \']]>\', $content);
// apply you own string replace here
echo $content;
}
使用筛选器和操作:
/**
* Plugin Name: Your_awsome_inlinestyle
* Plugin URI: http://wordpress.stackexchange.com/questions/72681/how-to-add-an-inline-style-to-the-p-tag-outputted-in-the-content-using-php
* Description: See link to plugin
* Version: 0.1
* Author: Ralf Albert
* Author URI: http://yoda.neun12.de
* Text Domain:
* Domain Path:
* Network:
* License: GPLv3
*/
add_action( \'plugins_loaded\', \'init_inlinestyler\', 10, 0 );
function init_inlinestyler(){
add_filter( \'the_content\', \'add_inlinestyle_to_p_tag\', 10, 1 );
}
function add_inlinestyle_to_p_tag( $content = null ){
if( null === $content )
return $content;
return str_replace( \'<p>\', \'<p style="color:red">\', $content );
}
Update
如果只想对特殊作业使用筛选器而不是全局筛选,请在需要的位置添加筛选器,然后再次删除。首先定义过滤器回调:
function insert_inline_style( $content = null ){ ... }
现在我们必须添加过滤器,因此the_content
将被筛选:
<?php
// add the filter
add_filter( \'the_content\', \'insert_inline_style\', 10, 1 );
// output the post content
the_content();
// remove the filter if it is not longer needed
remove_filter( \'the_content\', \'insert_inline_style\' );
所以你不必到处乱摆弄get_the_content()
并且可以在需要时轻松重用回调。我可能会迟到,但请用这个:
ob_start();
the_content();
$content = ob_get_clean();
然后在任何地方调用$content。基本上,\\u内容保存在内存中(另一个时间维度),直到ob_get_clean()
被调用。上面说,the_content()
将格式保持为get_the_content()
没有。这就是为什么许多答案会试图过滤get_the_content()
, 这很麻烦。