Is there a way to force The Loop to show only tags from the content of a post, while ignoring all other tags (
h2
, p
, hr
... )?
$hm_intro = new WP_query(array(
'p' => 375,
'post_type' => 'any',
'posts_per_page' => 1
));
if($hm_intro->have_posts()) : while($hm_intro->have_posts()) : $hm_intro->the_post();
//Here I want display only the from the_content().
endwhile;
wp_reset_postdata();
endif;
?>
Answer
You can parse all the H1s out of the post content with domDocument:
if($hm_intro->have_posts()) : while($hm_intro->have_posts()) : $hm_intro->the_post();
$dom = new domDocument;
$dom->loadHTML(get_the_content());
$headings = $dom->getElementsByTagName('h1');
foreach ($heading as $h){
echo '' . $h->textContent . '
';
}
endwhile;
No comments:
Post a Comment