如何在不显示图库代码的情况下显示页面的内容

时间:2013-01-14 作者:Larry B

我正在使用下面的按名称获取页面内容并输出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> 标签。

我怎样才能做到这一点?

2 个回复
最合适的回答,由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.

SO网友:Dan

你只需要申请preg_replace 删除所有匹配的模式\\[gallery[^\\]]+\\] 从您的内容(在扩展所有内容之前)。

结束

相关推荐

Too many actions/filters!

这里是wordpress的新成员。动作/过滤器的概念本身并不难理解。令我不知所措的是大量可用的操作和过滤器。当我阅读教程/指南时,他们会说“只需将此功能添加到wp\\U head操作或after\\U setup\\u主题”。如果没有这些教程,我究竟如何知道将该函数与该操作挂钩?作为一个初学者,我怎么会知道什么是合适的操作?有没有关于如何导航的建议?谢谢