Cómo mostrar publicidad en categorías específicas del post

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

itxel

Delta
Verificación en dos pasos activada
Verificado por Whatsapp
Hola chic@s.

He conseguido el código del function.php que te ayuda a mostrar una publicidad luego de un número de palabras del texto del post que tú indiques, pero lo que necesito es que además, esa pubicidad se muestre en determinadas categorías. Alguien sabe qué debo añadir para mostrar la publicidad en las categorías que yo indique?

Este es el código:

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 500 characters
  $after_character = 500;
  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 = '
      <!-- PUT YOUR AD HERE -->
    ';
    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');

Gracias de antemano
 
Si no quieres complicarte había un plugin Adman o New Adman que hace eso y mucho mas. Así si cambias de Theme seguirá todo igual.
 
Yo utilizo algo como <? if (in_category('14')).....
 
Gracias por las respuesta jose26, pero busco implementarlo sin plugin.

Sabogal, supongo que ese código puede valer o algo como if ( in_category('14','20','50')); pero donde debo situarlo exactamente?

Saludos y nuevamente gracias.
 
Seria riesgoso poner un bloque cuadrado de anuncios de texto en la mitad del contenido del post, alineado a la izquierda? habria que separar bien? como lo ven?
 
Prueba con esto: las categorias son 9 y blue-cheese

Insertar CODE, HTML o PHP:
if (is_category( array( 9, 'blue-cheese') )) {
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 500 characters
  $after_character = 500;
  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 = '
      <!-- aqui el codigo de su adsense -->
    ';
    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');}
 
Gracias kilogramo, lo he probado modificando mis categorías pero no las muestra :ambivalence:
 
Esta genial, tu codigo mucha gracias me ha servido bastante.. ojala me sirva para mejorar las ganancias.
 
Atrás
Arriba