/* 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); } Shahid Khaqan Abbasi
Follow
WhatsApp

PTI chairman Imran Khan says former PM told a lie everywhere

News Desk
2017-11-09

CHITRAL: PTI chairman Imran Khan has said that former prime minister Nawaz Sharif had told a lie everywhere because he...

PM strongly condemns terrorist attack in Quetta

News Desk
2017-11-09

Shahid Khaqan Abbasi says such acts cannot shake our resolve in fight against terrorism. Prime Minister Shahid Khaqan Abbasi has...

Govt working with full dedication to mainstream FATA, says PM Abbasi

News Desk
2017-11-08

Govt provides equal opportunities to all federating units to benefit from CPEC: PM Prime Minister Shahid Khaqan Abbasi says the...

PM Abbasi visits Pakistan Navy Hqs

News Desk
2017-11-08

ISLAMABAD: Prime Minister Shahid Khaqan Abbasi Visited the Pakistan Navy headquarters on Wednesday to get a briefing on the operational...

Federal Cabinet meets today

News Desk
2017-11-08

ISLAMABAD: An important meeting of the federal cabinet will be held in Islamabad today (Wednesday) with Prime Minister Shahid Khaqan...

PM briefed on operational preparedness of Pakistan Navy

News Desk
2017-11-08

Prime Minister Shahid Khaqan Abbasi visited Naval Headquarters in Islamabad on Wednesday. On arrival, he was received by Naval Chief...

Assemblies will only be dissolved on expiry of tenure: PML-N leaders

News Desk
2017-11-07

ISLAMABAD: Former prime minister Nawaz Sharif presided over an important meeting of the Pakistan Muslim League-Nawaz (PML-N) on Monday. The...

Pakistan denounces killing of diplomat in Jalalabad

News Desk
2017-11-07

ISLAMABAD: Pakistan has on Tuesday strongly condemned the killing of its diplomatic official Nayyar Iqbal Rana serving at the Consulate...

Pakistan lodges strong protest with Afghanistan over killing of diplomatic official in Jalalabad

News Desk
2017-11-07

Pakistan has strongly condemned the killing of its diplomatic official Nayyar Iqbal Rana serving at the Consulate General of Pakistan...

No big differences within party: Shahid Khaqan Abbasi

News Desk
2017-11-07

ISLAMABAD: Prime Minister Shahid Khaqan Abbasi has on Tuesday said that there were no big differences within the ruling Pakistan...

Previous Next