我正在使用下面的按名称获取页面内容并输出html
$my_wp_query = new WP_Query();
$page = $my_wp_query->query(
array(
\'post_type\' => \'page\',
\'name\' => \'some-page-slug\'
)
);
$page = $page[0];
$page_data[\'content\'] = apply_filters(\'the_content\', $page->post_content);
现场
$page->post_content
unmodified包含一个字符串,其中包含我在后端键入的内容以及对表单中库的引用
[gallery="1,2,3,4"]
在字符串的末尾。当我在
apply_filters(\'the_content\', $page->post_content)
方法,它恢复HTML标记,例如
<p>
元素,但它还包括库代码,它有自己的样式和相当长的标记,如下所示。
<p>Residential Gallery. Etiam lobortis sapien vel erat sodales et suscipit leo pretium. Pellentesque a arcu sed velit congue sagittis ac vel erat. Curabitur non felis leo, vel vestibulum purus. Donec eget velit eget magna rutrum facilisis. In hac habitasse platea dictumst.</p>
<p>Quisque sit amet pellentesque risus. Aliquam erat volutpat. Ut at magna justo, tincidunt viverra odio. Pellentesque a arcu sed velit congue sagittis ac vel erat. Curabitur non felis leo, vel vestibulum purus. Donec eget velit eget magna rutrum facilisis. In hac habitasse platea dictumst. Quisque sit amet pellentesque risus. Aliquam erat volutpat.</p>
<p>Ut at magna justo, tincidunt viverra odio. Curabitur non felis leo, vel vestibulum purus. Donec eget velit eget magna rutrum facilisis. In hac habitasse platea dictumst. Quisque sit amet pellentesque risus. Aliquam erat volutpat. Ut at magna justo, tincidunt viverra odio.</p>
<style type=\'text/css\'>
#gallery-1 {
margin: auto;
}
#gallery-1 .gallery-item {
float: left;
margin-top: 10px;
text-align: center;
width: 33%;
}
#gallery-1 img {
border: 2px solid #cfcfcf;
}
#gallery-1 .gallery-caption {
margin-left: 0;
}
</style>
<!-- see gallery_shortcode() in wp-includes/media.php -->
<div id=\'gallery-1\' class=\'gallery galleryid-0 gallery-columns-3 gallery-size-thumbnail\'><dl class=\'gallery-item\'>
<dt class=\'gallery-icon\'>
<a href=\'http://local.definitive-final2.co.uk/blog/residential/georgian-house/georgian1/\' title=\'georgian1\'><img width="150" height="150" src="http://local.definitive-final2.co.uk/blog/wp-content/uploads/2013/01/georgian1-150x150.jpg" class="attachment-thumbnail" alt="georgian1" /></a>
</dt></dl><dl class=\'gallery-item\'>
<dt class=\'gallery-icon\'>
<a href=\'http://local.definitive-final2.co.uk/blog/residential/georgian-house/georgian2/\' title=\'georgian2\'><img width="150" height="150" src="http://local.definitive-final2.co.uk/blog/wp-content/uploads/2013/01/georgian2-150x150.jpg" class="attachment-thumbnail" alt="georgian2" /></a>
</dt></dl><dl class=\'gallery-item\'>
<dt class=\'gallery-icon\'>
<a href=\'http://local.definitive-final2.co.uk/blog/residential/georgian-house/georgian3/\' title=\'georgian3\'><img width="150" height="150" src="http://local.definitive-final2.co.uk/blog/wp-content/uploads/2013/01/georgian3-150x150.jpg" class="attachment-thumbnail" alt="georgian3" /></a>
</dt></dl><br style="clear: both" /><dl class=\'gallery-item\'>
<dt class=\'gallery-icon\'>
<a href=\'http://local.definitive-final2.co.uk/blog/residential/georgian-house/georgian4/\' title=\'georgian4\'><img width="150" height="150" src="http://local.definitive-final2.co.uk/blog/wp-content/uploads/2013/01/georgian4-150x150.jpg" class="attachment-thumbnail" alt="georgian4" /></a>
</dt></dl><dl class=\'gallery-item\'>
<dt class=\'gallery-icon\'>
<a href=\'http://local.definitive-final2.co.uk/blog/residential/paris-house/paris4/\' title=\'paris4\'><img width="150" height="150" src="http://local.definitive-final2.co.uk/blog/wp-content/uploads/2013/01/paris4-150x150.jpg" class="attachment-thumbnail" alt="paris4" /></a>
</dt></dl>
<br style=\'clear: both;\' />
</div>
我只想检索HTML文本
<p>
\'而不是画廊的
<style>
和
<div>
标签。
我怎样才能做到这一点?
最合适的回答,由SO网友:s_ha_dum 整理而成
我可能误解了你,但我认为你想做的是strip_shortcodes
$page_data[\'content\'] = strip_shortcodes($page->post_content);
$page_data[\'content\'] = apply_filters(\'the_content\', $page_data[\'content\']);
或者,您可以有条件地将该函数挂钩到
the_content
如上所示
Codex Page.