寻找一个PHP函数(非jQuery或wpautop修改)方法从wordpress中删除<p></p>
。
我试过这个,但它不起作用:
function cleanup_shortcode_fix($content) {
$array = array (
'<p>[' => '[',
']</p>' => ']',
']<br />' => ']',
']<br>' => ']'
);
$content = strtr($content, $array);
return $content;
}
add_filter('the_content', 'cleanup_shortcode_fix');
尝试在functions.php
文件中插入此代码:
remove_filter( 'the_content', 'wpautop' );
add_filter( 'the_content', 'wpautop', 99 );
add_filter( 'the_content', 'shortcode_unautop', 100 );
add_filter('the_content', 'cleanup_shortcode_fix', 10);
我发现如果你指定10作为优先级它是有效的;没有其他数字可行。
这是一个老问题,但我今天解决了这个问题并且认为我会分享。
在我的情况下,我基本上想要删除所有格式不佳的<p>
和<br>
标签,但是然后你想要正确地添加它们,以便短代码中的文本被正确格式化。
/*
* Half column shortcode
*/
function custom_shortcode_half_column( $atts, $content = '') {
$content = custom_filter_shortcode_text($content);
return '<div class="half-column">'. $content .'</div>';
}
add_shortcode( 'half-column', 'custom_shortcode_half_column' );
/*
* Utility function to deal with the way WordPress auto formats text in a shortcode.
*/
function custom_filter_shortcode_text($text = '') {
// Replace all the poorly formatted P tags that WP adds by default.
$tags = array("<p>", "</p>");
$text = str_replace($tags, "\n", $text);
// Remove any BR tags
$tags = array("<br>", "<br/>", "<br />");
$text = str_replace($tags, "", $text);
// Add back in the P and BR tags again, remove empty ones
return apply_filters('the_content', $text);
}
这应该是我认为WordPress解析短代码$ content参数的默认方式。
也许一个正则表达式可以工作:
$string=preg_replace_('/<p>\s*</p>/', '', $string);
这应该取代任何<p></p>
没有任何东西或只是空格中的任何东西,从而删除它们。
将正则表达式应用于HTML代码时,最好先删除HTML的\r\n
,因为它们会阻止正则表达式的运行。
您应该增加过滤器的优先级。
这应该工作
add_filter('the_content', 'cleanup_shortcode_fix', 1);
你可以删除
一天进入
<?php echo $post->post_content; ?>
而不是the_content()
你需要的是jquery和php的混合...这是唯一的工作方式 我觉得工作得很好。我在我的网站上有教程但是 为了保持内部的东西在这里去
jQuery: 将此包含在您已经入队的某个JS文件中
jQuery(function($){
$('div#removep > p').filter(function() {
return $.trim($(this).text()) === '' && $(this).children().length == 0
})
.remove()
})
您可以在以后使用的短代码: 在你的functions.php或包含的文件中
function sght_removep( $atts, $content = null ) {return '<div id="removep">'.do_shortcode($content).'</div>';}
add_shortcode('removep', 'sght_removep');
现在你可以包装这样的特定东西:
[removep]
Some text i write directly in wordpress wysiwyg
<p></p> <-- this would get removed
[/removep]
这个解决方案需要一些知道但它的工作原理! 希望这可以帮助...