Valentin
Valentin

Reputation: 61

How to filter the_content() function in php to only return one certain div class?

Using the function the_content() return the whole content of a post. However I only want to return the content of one div class of this post. Is there a way to filter the function or are other methods possible to achieve the same result?

My content

<div class="hi"> Hello </div>
<div class="bye"> good bye</div>

Using the_content() returns

Hello
good bye

How I only want this returned

Hello

Upvotes: 0

Views: 643

Answers (1)

AWS PS
AWS PS

Reputation: 4710

You can use

$doc = new DOMDocument();
$content = get_the_content(); 
$doc->loadHTML($content);
$element = $doc->getElementByClass('hi');
$elementContent = $doc->textContent();

Upvotes: 2

Related Questions