Reputation: 61
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
Reputation: 4710
You can use
$doc = new DOMDocument();
$content = get_the_content();
$doc->loadHTML($content);
$element = $doc->getElementByClass('hi');
$elementContent = $doc->textContent();
Upvotes: 2