/* Plugin Name: Optimized Inline Related Articles v5.0 (Ads Removed) */ add_filter('the_content', 'optimized_inline_related_articles_v5', 99); function optimized_inline_related_articles_v5($content) { if (!is_single() || !is_main_query()) return $content; global $post; $used_posts = [$post->ID]; $paragraphs = explode('

', $content); $total_paragraphs = count($paragraphs); if ($total_paragraphs < 6) return $content; // ------------------------ // TAG BASED RELATED POSTS // ------------------------ $tags = wp_get_post_tags($post->ID); if (!$tags) return $content; $tag_ids = wp_list_pluck($tags, 'term_id'); $related_query = new WP_Query([ 'tag__in' => $tag_ids, 'post__not_in' => $used_posts, 'posts_per_page' => 15, 'ignore_sticky_posts' => 1, ]); $related_posts = []; if ($related_query->have_posts()) { while ($related_query->have_posts()) { $related_query->the_post(); $post_tags = wp_get_post_tags(get_the_ID()); $shared_tags = count(array_intersect($tag_ids, wp_list_pluck($post_tags,'term_id'))); $related_posts[] = [ 'id' => get_the_ID(), 'title' => get_the_title(), 'link' => get_permalink(), 'shared_tags' => $shared_tags ]; } wp_reset_postdata(); } if (empty($related_posts)) return $content; usort($related_posts, fn($a,$b) => $b['shared_tags'] - $a['shared_tags']); // ===================================================== // INLINE INSERT (TOP SECTION) // ===================================================== $top_positions = [11,15,19]; $count = 0; foreach($top_positions as $pos){ if(!isset($related_posts[$count])) break; if($pos >= $total_paragraphs) continue; $rp = $related_posts[$count]; $inline_html = "
Read More: {$rp['title']}
"; $paragraphs[$pos] .= $inline_html; $count++; } // ===================================================== // BOTTOM INSERT (UNCHANGED LOGIC, NO ADS FILTER) // ===================================================== $bottom_positions = []; for($i=floor($total_paragraphs*0.45); $i<$total_paragraphs; $i++){ $bottom_positions[] = $i; } $max_bottom = min(3, count($related_posts)-$count); if($max_bottom > 0 && count($bottom_positions) > 0){ $interval = max(1, floor(count($bottom_positions)/$max_bottom)); for($b=0;$b<$max_bottom;$b++){ $pos_index = $b * $interval; if(!isset($bottom_positions[$pos_index])) break; $pos = $bottom_positions[$pos_index]; if(!isset($related_posts[$count])) break; $rp = $related_posts[$count]; $inline_html = "
Read More: {$rp['title']}
"; $paragraphs[$pos] .= $inline_html; $count++; } } return implode('

', $paragraphs); } Border Management
Follow
WhatsApp

Af-Pak first meeting of Joint Technical Border Management Group in Kabul

News Desk
2016-07-22

ISLAMABAD: (APP) The first meeting of Pakistan-Afghanistan joint technical working group on border management and related security issues will be...

US announces support for Pakistan s Af-Pak border management

News Desk
2016-07-02

ISLAMABAD (APP): United States Special Representative for Afghanistan and Pakistan Ambassador Richard Olson on Saturday recognized that Pakistan’s role in...

Pakistan – Afghanistan agree on border management mechanism

News Desk
2016-06-21

ISLAMABAD: (APP) Foreign Secretary Aizaz Ahmad Chaudhry on Monday said Pakistan and Afghanistan have agreed on a mechanism for border...

Af-Pak border management: Pakistan – Afghanistan delegations meets

News Desk
2016-06-20

ISLAMABAD, June 20 (APP): Af-Pak border management: Pakistan – Afghanistan delegations meets Advisor to Prime Minister on Foreign Affairs Sartaj...

Af-Pak: Why Afghan Deputy FM is arriving in Islamabad

News Desk
2016-06-18

ISLAMABAD: (APP) Afghan Deputy Foreign Minister Hekmat Khalil Karzai will head a delegation to Islamabad on Monday for discussions on...

TTP being supported by Afghan state institutions : Defence Minister

News Desk
2016-06-17

ISLAMABAD: While expressing his concerns over the unprovoked firing by Afghan forces at Torkham, Defense Minister Khawaja Asif said that...

Afghan Refugees will have to go back home now : Tariq Fatemi

News Desk
2016-06-17

ISLAMABAD: (APP) Special Assistant to the Prime Minister on Foreign Affairs, Syed Tariq Fatemi on Friday said that Pakistan was...

Pakistan-Afghanistan Relations: Why border management is must for both sides

News Desk
2016-06-16

ISLAMABAD, June 16 (APP): Pakistan-Afghanistan Relations: Why border management is must for both sides Defence Minister Khawaja Mohammad Asif Thursday...

Pakistan – Afghanistan border management: Challanges and Way Forward

News Desk
2016-06-07

ISLAMABAD: While taking concrete measures to improve the border management between Pakistan and Afghanistan, a new mechanism has been imposed...

Pak Army Angor Adda Check Post handed over to Afghanistan

News Desk
2016-05-22

RAWALPINDI: DG ISPR Maj. Gen. Asim Bajwa said that Angor Adda Check Post located near Pak-Afghan border has been handed...