División de post con publicidad por categorías

  • Autor Autor itxel
  • Fecha de inicio Fecha de inicio
itxel

itxel

Delta
Verificación en dos pasos activada
Verificado por Whatsapp
Hola chicos.

Tengo un código en mi function.php con el cual puedo dividir el texto de un post para que en esa división se muestre un banner de publicidad. Ahora, necesito que al hacer esa división si es la categoría a y b se muestre un banner y si es la categoría c se muestre otro y si no indico la categoría se muestre uno genérico. No tengo mucha experiencia en PHP por eso recurro a vosotros para ver si me podéis ayudar.

Os dejo el código del que dispongo.

Saludos

PHP:
function inject_ad_text_after_n_chars($content) {
// only do this if post is longer than 1000 characters
$enable_length = 1000;
// insert after the first </p> after 160 characters
$after_character = 160;
if (is_single() && strlen($content) > $enable_length) {
$before_content = substr($content, 0, $after_character);
$after_content = substr($content, $after_character);
$after_content = explode('</p>', $after_content);
$text = '


    <center><a href="http://www.misite.com/enlace"       target="_blank"><img src="http://www.misite.com/banner.jpg" alt="http://www.misite.com/banner.jpg" /></a></center>&nbsp

';
array_splice($after_content, 1, 0, $text);
$after_content = implode('</p>', $after_content);
return $before_content . $after_content;
}
else {
return $content;
}
}
 
Hola.

Muchas gracias por responder. Lo he intentado con este pero me da error en un string...

PHP:
function inject_ad_text_after_n_chars($content) {
// only do this if post is longer than 1000 characters
$enable_length = 1000;
// insert after the first </p> after 160 characters
$after_character = 160;
if (is_single() && strlen($content) > $enable_length) {
$before_content = substr($content, 0, $after_character);
$after_content = substr($content, $after_character);
$after_content = explode('</p>', $after_content);
$text = '

   if ( in_category( 1,3 ) ) {
    $text = '<a href="http://www.misite.com/enlace"       target="_blank"><img src="http://www.misite.com/banner1.jpg" alt="http://www.misite.com/banner1.jpg" /></a>';
} elseif ( in_category( 38158 ) ) {
    $text = '<a href="http://www.misite.com/enlace"       target="_blank"><img src="http://www.misite.com/banner2.jpg" alt="http://www.misite.com/banner2.jpg" /></a>';
} else {
    $text = '<a href="http://www.misite.com/enlace"       target="_blank"><img src="http://www.misite.com/banner3.jpg" alt="http://www.misite.com/banner3.jpg" /></a>';
}


';
array_splice($after_content, 1, 0, $text);
$after_content = implode('</p>', $after_content);
return $before_content . $after_content;
}
else {
return $content;
}
}


---------- Post agregado el 13-may-2013 hora: 10:29 ----------

Bueno chicos, buscándome la vida luego de la pista ofrecida por Seen y luego de algunos errores ya he conseguido que funcione. Así que lo comparto por si alguien más lo necesita.

PHP:
function inject_ad_text_after_n_chars($content) {
// only do this if post is longer than 1000 characters
$enable_length = 1000;
// insert after the first </p> after 160 characters
$after_character = 160;
if (is_single() && strlen($content) > $enable_length) {
$before_content = substr($content, 0, $after_character);
$after_content = substr($content, $after_character);
$after_content = explode('</p>', $after_content);
if ( in_category( 1 ) ) {
    $text = '<center><a href="http://www.misite.com/enlace"       target="_blank"><img src="http://www.misite.com/banner1.jpg" alt="http://www.misite.com/banner1.jpg" /></a></center>';
} elseif ( in_category( 3 ) ) {
    $text = '<center><a href="http://www.misite.com/enlace"       target="_blank"><img src="http://www.misite.com/banner2.jpg" alt="http://www.misite.com/banner2.jpg" /></a></center>';    
} elseif ( in_category( 4 ) ) {
    $text = '<center><a href="http://www.misite.com/enlace"       target="_blank"><img src="http://www.misite.com/banner3.jpg" alt="http://www.misite.com/banner3.jpg" /></a></center>';
} else {
    $text = '<center><a href="http://www.misite.com/enlace"       target="_blank"><img src="http://www.misite.com/bydefault.jpg" alt="http://www.misite.com/bydefault.jpg" /></a></center>';
}
array_splice($after_content, 1, 0, $text);
$after_content = implode('</p>', $after_content);
return $before_content . $after_content;
}
else {
return $content;
}
}
add_filter('the_content', 'inject_ad_text_after_n_chars');
 
Última edición:
Me alegro que te haya salido 🙂
 
Excelente, lo guardare para con este implementar el de mostrar adsense de acuerdo al autor del post
 
Atrás
Arriba