pull out images in wordpress post

2019-08-30 19:05发布

问题:

Looking to automatically separate the images and text from my wordpress post <?php the_content(); ?> so I can put them in two different locations.

Not sure the best method to do this but have tried preg_replace approach but cant seem to figure out the right input

<?php
 // get the content
 $block = get_the_content();

 // check and retrieve blockquote
if(preg_match ... 

?>

回答1:

It's answered here on wordpress.stackexchange.

$content = get_the_content();
$content = preg_replace("/<img[^>]+\>/i", " ", $content);          
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;

To get the images from the inline post content this article will help.

preg_match('#(<img.*?>)#', $content, $results);


回答2:

You need to build a new function with the_content() to filter media and string from post content.

To do it, You should to know php well.



标签: php wordpress