separating the images and text content from Wordpr

2019-09-14 20:46发布

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

标签: php wordpress
1条回答
Fickle 薄情
2楼-- · 2019-09-14 21:24

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

$document = new DOMDocument();
$document->loadHTML($content);
$images = $document->getElementsByTagName('img');
查看更多
登录 后发表回答