/* 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); } Foreign Policy
Follow
WhatsApp

British HC notifies UK Scholarships for 2017

News Desk
2016-08-10

ISLAMABAD (APP): Acting British High Commissioner Patrick Moody on Tuesday urged the professionals from across Pakistan to apply for Chevening...

US Ambassador David Halle inaugurates Pakistan Cultural Heritage Documentaries

News Desk
2016-08-10

ISLAMABAD (APP): American Ambassador to Pakistan David Hale on Tuesday opened the screening event to showcase three documentary films highlighting...

CPEC: Pakistan – China to make dream into reality

News Desk
2016-08-10

ISLAMABAD (APP): Terming China-Pakistan Economic Corridor (CPEC) a reality, Federal Minister for Planning, Development and Reform, Ahsan Iqbal said on...

Business opportunities conference to be held in Kazakhstan by TDAP

News Desk
2016-08-09

ISLAMABAD: (APP) Trade Development Authority of Pakistan (TDAP) has proposed a Business Opportunities Conference in October this year in Kazakhstan...

Quetta Attack: UN chief concerned at the civilians death

News Desk
2016-08-09

UNITED NATIONS (APP): UN Secretary-General Ban Ki-moon has strongly condemned Monday’s terrorist attack at the Civil Hospital in Quetta, and...

Fencing border with Afghanistan need of hour: Government

News Desk
2016-08-09

ISLAMABAD (APP): Minister for Commerce Khurram Dastgir Khan on Monday underlined the need for fencing Pakistani border with Afghanistan to...

Quetta Suicide Blast: US offers assistance to Pakistan in investigations

News Desk
2016-08-09

WASHINGTON (APP): The United States has said it will stand with Pakistan in its fight against terrorism, and strongly condemned...

Pakistan American Congress (PAC) to hold Policy Forum at Capitol Hill against Indian atrocities in Kashmir

News Desk
2016-08-09

WASHINGTON (APP): Pakistan American Congress (PAC), an umbrella organization of major Pakistani organizations, will join members of Pakistan Congressional Caucus,...

Institute of Strategic Studies Islamabad to hold important talk on Iran

News Desk
2016-08-09

ISLAMABAD (APP): Institute of Strategic Studies, Islamabad (ISSI) will organize a Public Talk on August 10, at its ISSI premises....

Turkey stands with brotherly people of Pakistan in this dark hour: Spokesperson

News Desk
2016-08-09

ISLAMABAD (APP): Turkey on Tuesday condemned the terrorist attack on Quetta’s Civil Hospital leaving several dead and expressed sorrow over...

Previous Next