separating the images and text content from Wordpr

2019-09-14 20:45发布

问题:

I have a Wordpress post which I would like to strip the images and text content out in 2 separate parts. I wan't to be able to control both elements separately.

current attempt

<?php
    $content = wpautop($content); // Add paragraph-tags
    $content = str_replace('<p></p>', '', $content); // remove empty paragraphs
    $content = preg_replace('/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU', '\1\2\3', $content); // remove paragraphs around img tags
    echo $content;
?> 

I was able to put

tags on the content but ot sure how to take out elements and bring each type back

回答1:

You can do something like this to grab all img tags:

$document = new DOMDocument();
$document->loadHTML($content);
$images = $document->getElementsByTagName('img');


标签: php wordpress