2014-12-04 21 views
1

以下是在第1,第2,第3段之後放置廣告的代碼。但我想在我的wordpress文章的最後一段之前放置廣告。有沒有辦法做到這一點?在wordpress的最後一段之前放置廣告

<?php 

add_filter('the_content', 'prefix_insert_post_ads'); 

function prefix_insert_post_ads($content) { 

    $ad_code = '<div>Ads code goes here</div>'; 

    if (is_single() && ! is_admin()) { 
     return prefix_insert_after_paragraph($ad_code, 1, $content); 
    } 

    return $content; 
} 

// Parent Function that makes the magic happen 

function prefix_insert_after_paragraph($insertion, $paragraph_id, $content) { 
    $closing_p = '</p>'; 
    $paragraphs = explode($closing_p, $content); 
    foreach ($paragraphs as $index => $paragraph) { 

     if (trim($paragraph)) { 
      $paragraphs[$index] .= $closing_p; 
     } 

     if ($paragraph_id == $index + 1) { 
      $paragraphs[$index] .= $insertion; 
     } 
    } 

    return implode('', $paragraphs); 
} 
?> 
+0

爲我讀,這只是第一款之後加入廣告代碼。只需要統計段落的數量以及你在哪裏有$ index + 1做$ index = x,而X是段落的總數 - 最後一個。 – nunorbatista 2014-12-04 19:05:15

+0

「請儘快回覆我」不是很禮貌:-) – 2014-12-04 19:20:31

+0

確定pieter..actually我是一個新用戶在這裏..我會記住你的建議,從下次。 – 2014-12-06 13:11:31

回答

0

我在我的網站和etunescafe中設置了此代碼的AdSense代碼。您只需在您的網站functions.php中添加此代碼。在添加代碼之前,請替換您的或添加您的AdSense代碼。通過這篇文章將會很好。 How to Set Adsense Ads Right Before The Last Paragraph

function ads_added_above_last_p($text) { 
    if(is_single()) : 
     $ads_text = '<div class="a" style="text-align: center;">Your Adsense Code</div>'; 
     if($pos1 = strrpos($text, '<p>')){ 
      $text1 = substr($text, 0, $pos1); 
      $text2 = substr($text, $pos1); 
      $text = $text1 . $ads_text . $text2; 
     } 
    endif; 
    return $text; 
    } 
add_filter('the_content', 'ads_added_above_last_p'); 
+0

謝謝@etunescafe ...適用於我 – 2015-02-17 18:16:00

+0

基於您的鏈接的域名/ URL與您的用戶名相同或包含您的用戶名,您似乎已鏈接到您自己的網站。如果你這樣做,你*必須透露它是你的網站*。如果您不透露它是您自己的網站,則通常將其視爲垃圾郵件。請參閱:[**什麼意思是「良好」自我推銷?](// meta.stackexchange.com/q/182212)和[如何不成爲垃圾郵件製造者](// stackoverflow.com/help/promotion)。 – Makyen 2017-12-18 03:29:26

1
add_filter('the_content', 'ad_signal'); 
function ad_signal($content) 
{ 
    if (!is_single()) return $content; 
    $tent = get_the_content(); 
    $content = explode("</p>", $content); 
    $ss = count($content); 
    $ns = $ss-1; //**before last paragraph in wordpress ** 
    $new_content = ''; 
    for ($i = 0; $i < count($content); $i++) { 
     if ($i == $ns) { 

      $new_content.= ' <div style="text-align:center;">'; 
      $new_content.= '<h1> Your ads Here ! </h1>'; 
      $new_content.= '</div>'; 
     } 


     $new_content.= $content[$i] . "</p>"; 
    } 

    return $new_content; 
} 
+0

謝謝,作品像一個魅力! – 2016-09-21 08:54:11

+0

歡迎@NadeemKhan ^^ – 2017-01-20 15:04:53

0

嘿,夥計們,所以我碰到了同樣的問題,並設法解決它納入你的答案爲一體。

執行所有處理的函數被更改,以計算explode方法創建的所有段落。然後再添加一條if語句來評估您何時到達所需位置。

function prefix_insert_after_paragraph($insertion, $paragraph_id, $content) { 

    $closing_p = '</p>'; 

    $paragraphs = explode($closing_p, $content); 

    $last_paragraph_index = count($paragraphs); 

    foreach ($paragraphs as $index => $paragraph) { 

     if (trim($paragraph)) { 
      $paragraphs[$index] .= $closing_p; 
     } 

     if ($paragraph_id == $index + 1) { 
      $paragraphs[$index] .= $insertion; 
     } 

     else if($last_paragraph_index + $paragraph_id == $index + 1){ 
      $paragraphs[$index] .= $insertion; 
     } 

    } 


    return implode('', $paragraphs); 
} 

爲了使用新功能,您必須使用負數來調用它。

想想這個功能,如果你想從使用正數的內容的頂部開始,如果你想從內容的底部開始使用負數。

請記住,即使評估是針對您決定的地點(例如最後一段之前),添加到數組的條件使用我們都知道的基於零的索引。

這只是意味着要獲得第1段之後,您必須在調用該函數時使用數字2,並在最後一段必須使用-2之前等等。

這裏是我的例子中添加一個閱讀接下來的文章中,如果它存在於文章

add_filter('the_content', 'prefix_insert_next_article_banner'); 

function prefix_insert_next_article_banner($content) { 

    if (is_single() && ! is_admin() && get_next_post_link()) { 

      $next_banner = '<div class="next-banner"> <span> Read Next </span>'; 
      $next_banner .= '<a href="' . $prev = get_permalink(get_adjacent_post(false,'',false)) .'"> ' . get_the_title(get_adjacent_post(false,'',false)) .' </a>'; 
      $next_banner .= '</div>'; 

      return prefix_insert_after_paragraph($next_banner, -2, $content); 

    } 

    return $content; 
} 
相關問題