Σε blogs/CMS, συχνά θέλετε να εμφανίζετε διαφημίσεις σε συγκεκριμένες θέσεις μέσα στο περιεχόμενο (π.χ. μετά την 2η παράγραφο, πριν το τέλος).
<?php
function insertAds($content) {
// Μόνο για single posts
if (!is_single()) return $content;
$adCode = '<div class="ad-container"><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-XXXXXXXX" data-ad-slot="YYYYYYYY" data-ad-format="auto"></ins><script>(adsbygoogle = window.adsbygoogle || []).push({});</script></div>';
// Εισαγωγή μετά την 2η παράγραφο
$paragraphs = explode('</p>', $content);
$newContent = '';
foreach ($paragraphs as $index => $paragraph) {
$newContent .= $paragraph . '</p>';
if ($index === 1) { // Μετά το 2ο </p>
$newContent .= $adCode;
}
}
return $newContent;
}
add_filter('the_content', 'insertAds');
<?php
// single.php ή article.php
$adCode = '<div class="ad-slot">...AdSense/Ad Code...</div>';
function insertAdInContent($content, $adCode, $paragraphNumber = 2) {
$paragraphs = preg_split('/(<\/p>)/i', $content, -1, PREG_SPLIT_DELIM_CAPTURE);
$output = '';
$pCount = 0;
foreach ($paragraphs as $part) {
$output .= $part;
if (stripos($part, '</p>') !== false) {
$pCount++;
if ($pCount === $paragraphNumber) {
$output .= $adCode;
}
}
}
return $output;
}
// Χρήση
$articleContent = getArticleContent($postId); // Η δική σας συνάρτηση
echo insertAdInContent($articleContent, $adCode, 3); // Μετά την 3η παράγραφο
?>
Για theme-based προσέγγιση, καταχωρίστε widget areas:
<?php
// functions.php
function registerAdWidgets() {
register_sidebar([
'name' => 'In-Content Ad - After Paragraph 2',
'id' => 'in-content-ad-1',
'before_widget' => '<div class="widget-ad">',
'after_widget' => '</div>',
]);
register_sidebar([
'name' => 'In-Content Ad - Before Last Paragraph',
'id' => 'in-content-ad-2',
'before_widget' => '<div class="widget-ad">',
'after_widget' => '</div>',
]);
}
add_action('widgets_init', 'registerAdWidgets');
<?php
function shouldShowAd($postId, $userId = null) {
// Μην δείχνεις διαφημίσεις σε:
// - Premium users
// - Specific categories
// - Short posts
// - Mobile (αν το ad δεν είναι responsive)
$post = getPost($postId);
if ($post['word_count'] < 500) return false;
if (in_array($post['category_id'], [10, 15])) return false; // No-ads categories
if ($userId && isPremiumUser($userId)) return false;
return true;
}
// Στο template
if (shouldShowAd($postId, $currentUserId)) {
echo getAdCode('in-content-1');
}
?>
data-ad-format="auto" ή CSS media queries.IntersectionObserver).<style>
.ad-container {
margin: 20px 0;
text-align: center;
min-height: 90px; /* Αποφυγή layout shift */
}
.ad-label {
font-size: 0.75rem;
color: #999;
text-transform: uppercase;
margin-bottom: 8px;
display: block;
}
@media (min-width: 768px) {
.ad-container { min-height: 280px; }
}
</style>
<div class="ad-container">
<span class="ad-label">Διαφήμιση</span>
<ins class="adsbygoogle"
style="display:block"
data-ad-client="ca-pub-XXXXXXXX"
data-ad-slot="YYYYYYYY"
data-ad-format="auto"
data-full-width-responsive="true"></ins>
<script>(adsbygoogle = window.adsbygoogle || []).push({});</script>
</div>
Πνευματικά δικαιώματα © Γνώση Υπολογιστών Όλα τα δικαιώματα διατηρούνται.