Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2013-12-26 16:59:02

admi
Member
From: BY
Registered: 2007-12-10
Posts: 145
Website

To divide body for adsense code

Dear friends,

Is there any way to divide txp article body by words or characters to insert adsense code inside the main text dynamically?

There is a way to do that through rss_auto_excerpt by patagraphs:

<txp:rss_auto_excerpt paragraphs="2" showlinkwithexcerpt="0" />
<txp:php>
$body_array = explode('</p>', $thisarticle['body']);
for($i=2;$i <= count($body_array);$i++){
echo $body_array[$i];
}
</txp:php>
from http://adminway.ru/dinamicheskoe-razmeshchenie-koda-kontekstnoi-reklamy-na-textpattern.

In my case I am trying divide article body whose content is a <table></table> which is one paragraph. How to divide it by words or characters if it is possible at all?

Offline

#2 2014-01-01 18:05:32

etc
Developer
Registered: 2010-11-11
Posts: 5,393
Website GitHub

Re: To divide body for adsense code

The code you cite will produce unclosed <p> tags (html5 valid, but not so clean). To insert some content after (say) the 2nd and the 5th rows of a table, you can use etc_query, replacing <txp:body /> tag with

<txp:etc_query data='<txp:body />'
	replace="//tr[2]|//tr[5]$=<tr>adsense code</tr>" />

Offline

#3 2014-01-07 09:03:04

admi
Member
From: BY
Registered: 2007-12-10
Posts: 145
Website

Re: To divide body for adsense code

etc wrote #277742:

The code you cite will produce unclosed <p> tags (html5 valid, but not so clean). To insert some content after (say) the 2nd and the 5th rows of a table, you can use etc_query, replacing <txp:body /> tag with

<code><txp:etc_query data='<txp:body />' replace="//tr[2]|//tr[5]$=<tr>adsense code</tr>" />

Thanx. So far it does not work. The code shows up just above the content. Interestingly it creates TRs 3d ans 6th but they are empty. May be something wrong with xPath syntaxis?

Last edited by admi (2014-01-07 18:24:53)

Offline

#4 2014-01-07 11:13:50

etc
Developer
Registered: 2010-11-11
Posts: 5,393
Website GitHub

Re: To divide body for adsense code

What is the code you are trying to insert?

Edit: ah, ok, it misses <td>s, sorry, it should be something like <tr><td colspan='3'>adsense code</td></tr>. Also, if the adsense code contains ;, you must set separator="something_other_than_;" attribute of etc_query.

Last edited by etc (2014-01-07 15:40:59)

Offline

#5 2014-01-07 19:38:56

admi
Member
From: BY
Registered: 2007-12-10
Posts: 145
Website

Re: To divide body for adsense code

etc wrote #277858:

What is the code you are trying to insert?

Edit: ah, ok, it misses <td>s, sorry, it should be something like <tr><td colspan='3'>adsense code</td></tr>. Also, if the adsense code contains ;, you must set separator="something_other_than_;" attribute of etc_query.

I think the problem may be with quote "..."

like src="adsense.google.com/lalalala" 

in the adsense code as well. And may be some of js tags.

Last edited by admi (2014-01-07 19:46:21)

Offline

#6 2014-01-07 19:42:28

etc
Developer
Registered: 2010-11-11
Posts: 5,393
Website GitHub

Re: To divide body for adsense code

admi wrote #277884:

I think the problem may be with quote "..."

Yes, as usual.

Offline

#7 2014-01-07 20:00:36

admi
Member
From: BY
Registered: 2007-12-10
Posts: 145
Website

Re: To divide body for adsense code

etc wrote #277887:

Yes, as usual.

Well, i removed quotes from the code. It works without etc_query ok. Now I think the problem already in general in the tags <></> . Is there any way to handle js here?

Offline

#8 2014-01-07 20:10:50

etc
Developer
Registered: 2010-11-11
Posts: 5,393
Website GitHub

Re: To divide body for adsense code

Could you show me the adsense code, for testing? I have no problem inserting <script>.

Offline

#9 2014-01-07 20:20:04

admi
Member
From: BY
Registered: 2007-12-10
Posts: 145
Website

Re: To divide body for adsense code

etc wrote #277891:

Could you show me the adsense code, for testing? I have no problem inserting <script>.

Something like this:

<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
  <ins class="adsbygoogle"
     style="display:inline-block;width:728px;height:90px"
     data-ad-client="ca-pub-5859340735332440"
     data-ad-slot="7118039316"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

Offline

#10 2014-01-07 21:17:32

etc
Developer
Registered: 2010-11-11
Posts: 5,393
Website GitHub

Re: To divide body for adsense code

You should assign a value to async attribute, try

<txp:etc_query data='<txp:body />' separator=";;"
replace='//tr[2]$=<tr><td>
<script async="async" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
  <ins class="adsbygoogle"
     style="display:inline-block;width:728px;height:90px"
     data-ad-client="ca-pub-5859340735332440"
     data-ad-slot="7118039316"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
</td></tr>' />

Last edited by etc (2014-01-08 14:50:28)

Offline

#11 2014-01-08 18:54:08

admi
Member
From: BY
Registered: 2007-12-10
Posts: 145
Website

Re: To divide body for adsense code

etc wrote #277894:

You should assign a value to async attribute, try

<code>&lt;txp:etc_query data='&lt;txp:body /&gt;' separator=";;"...

Спасибо Олег! Thanx Oleg! TxP plugins are great but sometimes it takes lots of wits to grasp how they work!

Offline

#12 2014-01-09 20:29:35

etc
Developer
Registered: 2010-11-11
Posts: 5,393
Website GitHub

Re: To divide body for adsense code

admi wrote #277937:

Спасибо Олег! Thanx Oleg! TxP plugins are great but sometimes it takes lots of wits to grasp how they work!

You are welcome, надеюсь, всё работает? I agree etc_query is not the easiest to grok, but the problem in this case comes from XML specifications rather than from the plugin itself.

Offline

Board footer

Powered by FluxBB