Skip to content

Commit 7337340

Browse files
committed
Update Latest-Posts-Hover.php
1 parent 72c0141 commit 7337340

1 file changed

Lines changed: 20 additions & 19 deletions

File tree

widgets/Latest-Posts-Hover.php

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,7 @@ protected function _register_controls()
757757
$this->add_control(
758758
'remove_title',
759759
[
760-
'label' => esc_html__('Remove title', 'Latest-Posts-Hover'),
760+
'label' => esc_html__('Remove title(the first h2)', 'Latest-Posts-Hover'),
761761
'type' => \Elementor\Controls_Manager::SWITCHER,
762762
'label_on' => esc_html__('On', 'Latest-Posts-Hover'),
763763
'label_off' => esc_html__('Off', 'Latest-Posts-Hover'),
@@ -1328,28 +1328,29 @@ protected function render()
13281328

13291329
foreach ($posts as $post) {
13301330
$post_title = get_the_title($post->ID);
1331-
if (wp_is_mobile()) {
1332-
if($settings['remove_title']=='on'){
1333-
$post_content = str_replace($post_title, '', $post->post_content);
1334-
$post_content = wp_trim_words($post_content, $wordMobile);
1331+
$dom = new DOMDocument();
1332+
if($settings['remove_title']=='on'){
1333+
if (!empty($post->post_content)) {
1334+
@$dom->loadHTML($post->post_content); // Suppress warnings due to invalid HTML
1335+
$elements = $dom->getElementsByTagName('h2');
13351336

1336-
}
1337-
if($settings['remove_title']!='on'){
1337+
// Loop through h2 elements and remove them
1338+
foreach ($elements as $element) {
1339+
$element->parentNode->removeChild($element);
1340+
}
13381341

1342+
// Save the modified content back to the variable
1343+
$post->post_content = $dom->saveHTML();
1344+
}
1345+
}
1346+
if (wp_is_mobile()) {
13391347
$post_content = wp_trim_words($post->post_content, $wordPc);
1340-
}
1341-
} else {
1342-
if($settings['remove_title']=='on'){
1343-
$post_content = str_replace($post_title, '', $post->post_content);
1344-
$post_content = wp_trim_words($post_content, $wordPc);
1345-
1346-
}
1347-
if($settings['remove_title']!='on'){
1348+
1349+
}
1350+
else {
1351+
13481352
$post_content = wp_trim_words($post->post_content, $wordPc);
1349-
} }
1350-
if($settings['remove_title']=='on'){
1351-
$post_content = str_replace($post_title, '', $post_content);
1352-
}
1353+
}
13531354
$post_date = get_the_date('j F Y', $post->ID);
13541355
$post_link = get_permalink($post->ID);
13551356
$tags = get_the_tags($post->ID);

0 commit comments

Comments
 (0)