/* 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

Fully committed to FATA s mainstreaming, merger: PM

News Desk
2017-11-16

ISLAMABAD: Prime Minister Shahid Khan Abbasi on Thursday said federal and provincial governments were unanimously committed for mainstreaming of Federally...

PML-N govt will complete its tenure: PM Abbasi

News Desk
2017-11-15

Prime Minister Shahid Khaqan Abbasi talks to media men in Quetta. Prime Minister Shahid Khaqan Abbasi says the general elections...

PM Shahid Abbasi says rule should be as per law amp; constitution

News Desk
2017-11-15

QUETTA: Prime Minister Shahid Khaqan Abbasi has said that the rule should be according to the law and the constitution....

ECC meeting held in Islamabad with PM Shahid Khaqan in chair

News Desk
2017-11-14

ISLAMABAD: Prime Minister Shahid Khaqan Abbasi Monday chaired the meeting of the Economic Coordination Committee of the Cabinet here at...

Council of Common Interests agrees to hold elections on time

News Desk
2017-11-14

ISLAMABAD: The Council of Common Interests (CCI) has rejected PTI chairman Imran Khan’s demand for early polls. The CCI meeting...

CCI decides to hold next general elections on basis of provisional resultsof latest census

News Desk
2017-11-14

33rd session of Council of Common Interests held in Islamabad. The 33rd meeting of the Council of Common Interests was...

CCI meets to discuss delimitation of constituencies

News Desk
2017-11-13

A meeting of council of common interests is being held in Islamabad on Monday with Prime Minister Shahid Khaqan Abbasi...

Federal Cabinet approves maximum limit of retail prices of drugs

News Desk
2017-11-09

PM Shahid Khaqan Abbasi chairs meeting of the cabinet in Islamabad. The federal cabinet has allowed the State Bank of...

Federal Cabinet allows SBP to acquire shares of ZTBL and HBFCL

News Desk
2017-11-09

ISLAMABAD: Prime Minister Shahid Khaqan Abbasi today chaired a meeting of the Federal Cabinet at the Prime Minister’s Office. Following...

Afghan president phones PM, expresses condolence on diplomat’s murder

News Desk
2017-11-09

ISLAMABAD: Afghan President Muhammad Ashraf Ghani made a phone call to Prime Minister Shahid Khaqan Abbasi to express condolences on...

Previous Next