Skip to content

Instantly share code, notes, and snippets.

@DavidPeralvarez
Created September 13, 2018 15:38
Show Gist options
  • Save DavidPeralvarez/0ba1bc8dcc7b05acaad99e0db66e3662 to your computer and use it in GitHub Desktop.
Save DavidPeralvarez/0ba1bc8dcc7b05acaad99e0db66e3662 to your computer and use it in GitHub Desktop.
Cómo insertar un CTA dentro de las entradas automáticamente
<?php
/* CTA en el contenido */
add_filter( 'the_content', 'scv_cta_code' );
function scv_cta_code( $content ){
// Definimos nuestro CTA
$codeCTA = '
<div class="in-content-cta">
<p>Accede a todos los cursos y soporte del club</p>
<a href="#">Suscríbete por 30€/año</a>
</div>
';
// Comprobar que estamos en una entrada
if( is_singular( 'post' ) ):
return scv_insert_cta( $content, $codeCTA, 2 );
endif;
return $content;
}
function scv_insert_cta( $content, $codeCTA, $n ){
// Creamos un array de párrafos a partir del contenido
$paragraphs = explode( '</p>', $content );
$newContent = '';
foreach( $paragraphs as $i => $p ):
$newContent .= $p;
$newContent .= '</p>';
if( $n == $i+1 ):
$newContent .= $codeCTA;
endif;
endforeach;
return $newContent;
}
/* CTA en el contenido */
.in-content-cta{
padding: 50px 20px;
background-color: #f2f2f2;
text-align: center;
margin: 30px auto;
}
.in-content-cta p{
margin: 0 0 20px;
}
.in-content-cta a{
text-decoration: none;
color: white;
background-color: orange;
border-radius: 30px;
padding: 10px 30px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment