Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#13 2018-03-05 15:06:14

singaz
Member
Registered: 2017-03-12
Posts: 150

Re: Google Adsense inside an article?

etc, thank you! It’s easier.
For 4 days I’m trying to improve

${count(./p)=>5}
<txp:etc_query data='<txp:body />' separator="|\/|" 
replace='p[2]${count(./p)=>5}=<txp:output_form form="google_ads_1-one" />|\/|
p[6]${count(./p)=>10}=<txp:output_form form="google_ads_2-two" />' />

if the paragraph <(less than) 5, then the forms are not added.
If a paragraph > 5, then the first form after the second paragraph.
If a paragraph > 10, then the second form after the sixth paragraph.

p < 5, no form
bc. p > 5, form after p[2]
bc. p > 10, form after p[2] end after p[6]
bc. .....
bc. p > 30, form after p[2] after p[6] after p[10] after p[15] ....

Can not do it.
Can you offer any guidance?

Last edited by singaz (2018-03-05 15:10:12)


Sorry my horror English. I’m learning textpattern, I’m learning English

Offline

#14 2018-03-05 19:33:09

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

Re: Google Adsense inside an article?

p > 30, form after p[2] after p[6] after p[10] after p[15] ....

is not a very regular pattern, but let it be 2, 6, 10, 15, 20, ... ,10+5n, .... Are you going to insert 100 distinct forms in a 500 paragraph article? Or do we limit ourselves to, say, 10 forms? Then where should they be inserted?

This will insert <txp:output_form form="google_ads_n" /> form after each 5n - 3 article (i.e. 2, 7, 12) while the number of articles is greater than 5n:

<txp:etc_query data='<txp:body />'
    replace="p[position() mod 5 = 1]/preceding-sibling::p[4]$=<![CDATA[<txp:output_form form='google_ads_{##}' />]]>" />

Offline

#15 2018-03-05 21:37:54

singaz
Member
Registered: 2017-03-12
Posts: 150

Re: Google Adsense inside an article?

Super. Thank you! I will try to understand.

10 there are many. A maximum of 5.

It is best to randomly scatter a text after 3-5 paragraphs.

After last and pre-paragraph, do not set the form.

I do not know how to randomly organize, I set it fixed after 2 and 6 paragaphs.

Temporary works like this:

<txp:etc_if_date value="02..06|10..14|18..21|25..28" format="%d">
<txp:php>
$text=body();
$quantity=substr_count($text,'</p>');
if ($quantity>=10)
{
$parts=explode('</p>',$text);
$i=0;
while ($i<2) {
echo $parts[$i].'</p>';
 $i++;
}
echo output_form(array('form'=>'google_ads_1'));
$i=2;
while ($i<6) {
echo $parts[$i].'</p>';
 $i++;
}
echo output_form(array('form'=>'google_ads_2'));
$i=6;
while ($i<count($parts)-1) {
echo $parts[$i].'</p>';
 $i++;
}
}	
else 	
if ($quantity>=5)
{
$parts=explode('</p>',$text);
$i=0;
while ($i<2) {
echo $parts[$i].'</p>';
 $i++;
}
echo output_form(array('form'=>'google_ads'));
$i=2;
while ($i<count($parts)-1) {
echo $parts[$i].'</p>';
 $i++;
}
}
else 
{
echo($text);
}
</txp:php>
<txp:else />
<txp:php>
$text=body();
$quantity=substr_count($text,'</p>');
if ($quantity>=10)
{
$parts=explode('</p>',$text);
$i=0;
while ($i<3) {
echo $parts[$i].'</p>';
 $i++;
}
echo output_form(array('form'=>'google_ads_1'));
$i=3;
while ($i<7) {
echo $parts[$i].'</p>';
 $i++;
}
echo output_form(array('form'=>'google_ads_2'));
$i=7;
while ($i<count($parts)-1) {
echo $parts[$i].'</p>';
 $i++;
}
}	
else 	
if ($quantity>=5)
{
$parts=explode('</p>',$text);
$i=0;
while ($i<3) {
echo $parts[$i].'</p>';
 $i++;
}
echo output_form(array('form'=>'google_ads'));
$i=3;
while ($i<count($parts)-1) {
echo $parts[$i].'</p>';
 $i++;
}
}
else 
{
echo($text);
}
</txp:php>
</txp:etc_if_date>

Random makes etc_date:

<txp:etc_if_date value="02..06|10..14|18..21|25..28" format="%d">
form after 2 after 6
else
form after 3 after 7
</txp:etc_if_date>

Your example is better. I will try to understand


Sorry my horror English. I’m learning textpattern, I’m learning English

Offline

#16 2018-03-05 21:44:58

singaz
Member
Registered: 2017-03-12
Posts: 150

Re: Google Adsense inside an article?

In your example, can work with large text. Similar to what in wordpress


Sorry my horror English. I’m learning textpattern, I’m learning English

Offline

#17 2021-03-22 18:53:42

whocarez
Plugin Author
From: Germany/Ukraine
Registered: 2007-10-08
Posts: 305
Website GitHub Twitter

Re: Google Adsense inside an article?

I tried etc_query for repeated fading of an advertising of newsletter and social media stuff like that

<txp:etc_query data='<txp:body />' replace="p[position() mod 10 = 1]/preceding-sibling::p[4]$=<![CDATA[<txp:output_form form='newsletter-snippet' />]]>" />

but that ended up in an Undefined array key 1 error, because of within a div embedded youtube videos. So I ended up with an updated variant of this older direct php snippet.


$text = body();
$quantity = substr_count($text, '</p>');
if ($quantity >= 13) {
    variable(["name" => "splitted", "value" => "yes"]);
    $parts = explode('</p>', $text);

    $i = 0;

    while ($i < count($parts) - 1) {
        echo $parts[$i] . '</p>';

        if ($i >= 10 && $i % 10 == 0 && $i + 3 < count($parts) - 1) {
            echo output_form(['form' => 'newsletter-snippet']);
        }

        $i++;
    }
} else {
    echo $text;
}

So in this example the output form “newsletter-snippet” is faded in every tenth paragraph, if you have at least 13 paragraphs.

Offline

Board footer

Powered by FluxBB