Я использую ниже, чтобы получить содержимое страницы по ее имени и вывести 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
содержит строку с содержимым, которое я напечатал в бэкэнд, а также ссылку на галерею в форме в конце строки , Когда я запускаю его с помощью
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://img.wordpressask.com/filters/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://img.wordpressask.com/filters/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://img.wordpressask.com/filters/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://img.wordpressask.com/filters/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://img.wordpressask.com/filters/paris4-150x150.jpg" class="attachment-thumbnail" alt="paris4" /></a> </dt></dl> <br style='clear: both;' /> </div>
Я хотел бы получить только текст HTML с помощью <p>
и т. Д., А не теги галереи <style>
и <div>
.
Как я могу это достичь?
Возможно, я неправильно истолковал вас, но я думаю, что вы хотите сделать это через strip_shortcodes
$page_data['content'] = strip_shortcodes($page->post_content); $page_data['content'] = apply_filters('the_content', $page_data['content']);
В качестве альтернативы вы можете условно перехватить эту функцию на the_content
как показано на странице Codex .
Все, что вам нужно сделать, это применить preg_replace, чтобы удалить все шаблоны, соответствующие \[gallery[^\]]+\]
из вашего контента (прежде чем расширять все).