Wednesday, 30 November 2016

php - How can I show only tags in The Loop?



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

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...