如何在没有<p></p>
包装纸?
我目前使用的方法是:
<?php
$id=21;
$post = get_page($id);
$content = apply_filters(\'the_content\', $post->post_content);
echo $content;
?>
例如page ID = 21
具有以下内容:Some content
wordpress回声是什么<p>Some content</p>
非常感谢您的任何建议。
如何在没有<p></p>
包装纸?
我目前使用的方法是:
<?php
$id=21;
$post = get_page($id);
$content = apply_filters(\'the_content\', $post->post_content);
echo $content;
?>
例如page ID = 21
具有以下内容:Some content
wordpress回声是什么<p>Some content</p>
非常感谢您的任何建议。
您可以将其添加到函数中。例如php文件,
remove_filter (\'the_content\', \'wpautop\');
将从中删除段落格式the_content
所有情况下的模板标记。如果你将来需要它,你可以写;
<?php wpautop(the_content());?
> 在模板中添加格式,而无需重新添加过滤器。
使用echo get_the_content();
将绕过WordPresswpautop
也要过滤。
下面是我为您编写的一个小函数,您可以在模板文件中使用(将代码片段粘贴到functions.php中,请参阅下面的使用说明)。
function custom_content($args = \'\', $allow = \'\'){
if ($args == \'alltags\') {
$type = the_content();
} elseif ($args == \'notags\') {
$type = strip_tags(wpautop(get_the_content()), $allow);
} else {
$type = get_the_content();
}
echo $type;
}
用法:<?php custom_content();?> // no <p> tags, but <a>, <strong>, etc are preserved.
<?php custom_content(\'notags\');?> // strips all tags from content
<?php custom_content(\'notags\', \'<a>\');?> // strips all tags except <a>
<?php custom_content(\'notags\', \'<a><p><etc..>\');?> // strips all tags except <a> & <p>
<?php custom_content(\'alltags\');?> // all tags uses the_content();
现在,其中一些可能是多余的,但它说明了如何创建一个多用途函数以在模板中使用,该函数可以根据您的情况提供一些、全部或不提供格式。注意:如果有人想知道为什么wpautop
被包裹着get_the_content
以上,是由于get_the_content
没有通过wpautop
因此,如果要使用<p>
在使用自定义notags
需要添加的参数<p>
使用格式返回wpautop
.
基于ID获取页面内容的替代方法<p>
标签;
function get_content($id) {
$post = get_page($id);
$content = apply_filters(\'get_the_content\', $post->post_content);
echo $content;
}
用法:<?php get_content(21); ?>
您需要删除wpautop
过滤器ex:
<?php
$id=21;
$post = get_page($id);
//remove auto p filter
remove_filter (\'the_content\',\'wpautop\');
$content = apply_filters(\'the_content\', $post->post_content);
//return the filter for other posts/pages down the road
add_filter (\'the_content\',\'wpautop\');
echo $content;
?>