How to make the alternation of advertising Google Adsense and Yandex?
those. in the single.php template, you need to make adsense and rsya ad units appear 50/50, in turn, if adsense is shown, then rsya should not be shown, and also rsya.
- SeVlad, why are you getting into my topic? I used to tell you about this - word
|
3 answers
Here is an option with a file counter:
function adv_router(){ $counter_name = bloginfo('template_directory') . "adv_counter.txt"; if(!$f = fopen($counter_name, "w")){ fwrite($f,"0"); fclose($f); exit(); } $f = fopen($counter_name,"r"); $counterVal = fread($f, filesize($counter_name)); $route = ++$counterVal % 2; $f = fopen($counter_name, "w"); fwrite($f, $counterVal); fclose($f); if($route){ echo // гугл; }else{ echo // яндекс; } } UPD:
Here is such an option, with the addition of a counter in the options table:
function adv_router(){ global $wpdb; $counterVal = 0; $prefix = $wpdb->prefix; // или $prefix = $wpdb->base_prefix; // читает префикс из wp_config.php if(!get_option('adv_counter_01')){ $wpdb->insert( $prefix . 'options', array( 'option_name' => 'adv_counter_01', 'option_value' => '0'), array( '%s', '%d' ) ) }else{ $counterVal = ++(get_option('adv_counter_01')); update_option( 'adv_counter_01', $counterVal)); } if($counterVal % 2){ echo // гугл; }else{ echo // яндекс; } } - and if there is no right to create a file? Is there a simpler option? - word
- Is there access to the database? ftp? - Kirill Korushkin
- It can not be easier. Create a file not in the root, but in the folder where you have access. - Mrak
- @Kirill Korushkin, FTP access - word
- @word updated the answer - Kirill Korushkin
|
We generate random number 0 or 1. Further we check. If the number is 1 - we show Google ads, if 0 - we show Yandex ads.
<?php $r = mt_rand(0,1); if($r) { // показываем рекламу гугл } else { // показываем рекламу яндекс } ?> - so in this case, 50/50 because it will not work. This will be a random show. The difference in the show can be big - word
- If you approach the issue more thoroughly, you need to store the value of the last displayed advertisement somewhere. When the page loads again, retrieve this value. There may be, for example, a database. Either cookie if browser level 50/50 is sufficient. - Dmitry B.
- is done something like this: $ advert = get_option ('advert'); if ($ advert)? - word
- Please show me a small example of how to properly get from the database, and how to calculate the current value - word
- Competently sent - pavel1787mego
|
You need to use some kind of counter storage - a file or database. The quickest way is to use the WordPress option.
$advert = get_option( 'advert', 0 ); // значение по умолчанию - 0, если опция не существует в базе. if ( 0 === ( $advert % 2 ) ) { // остаток от деления на 2 = 0, показываем Google } else { // остаток от деления на 2 = 1, показываем Yandex } $advert++; // увеличиваем счетчик update_option( 'advert', $advert ); // сохраняем в базе |