Mostrar Botón Me Gusta en Post de Wordpress según Categoría

  • Autor Autor Alcarver
  • Fecha de inicio Fecha de inicio
Alcarver

Alcarver

Gamma
Verificación en dos pasos activada
Verificado por Whatsapp
¡Ha verificado su Paypal!
Hola,

En mi blog con Thesis Theme estoy utilizando el siguiente código para mostrar el botón me gusta al principio y al final de cada post:

Insertar CODE, HTML o PHP:
function fb_like() {
	if (is_single()) { ?>
		<iframe src="http://www.facebook.com/plugins/like.php?href=<?php echo urlencode(get_permalink($post->ID)); ?>&amp;layout=standard&amp;show_faces=false&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:450px; height:60px;"></iframe>

		<?php }
}
add_action('thesis_hook_after_post','fb_like');
add_action('thesis_hook_before_post','fb_like');

PERO hay una categoría (número 8) en la que quiero que se muestre el botón sólo al principio del post.

Una de las respuestas que me han dado es:

"You can create a condition (expand it) for the above that excludes from the above function && (!in_category('8')){ as described in the WP Codex on conditionals here. Then you could sort of do the reverse to get a function that says only if you are in category 8's posts then (same thing, different hook). You'd only use one add_action on that second function, since you only want the like before, but not after, the posts in that category."

😕

Soy más que novato en estos códigos y me están creciendo canas intentando hacerlo yo mismo.

Podéis ayudarme, Betas, por favor? :sorrow:
 
MODO CORTO, NO SE SI WORDPRESS LO SOPORTARÁ:

PHP:
function fb_like() {
	if (is_single()) { ?>
		<iframe src="http://www.facebook.com/plugins/like.php?href=<?php echo urlencode(get_permalink($post->ID)); ?>&amp;layout=standard&amp;show_faces=false&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:450px; height:60px;"></iframe>

		<?php }
}

add_action('thesis_hook_after_post','fb_like');
 if( !is_category( 8 )):
add_action('thesis_hook_before_post','fb_like');
endif;


MODO QUE FUNCIONA SEGURO PERO MÁS EXTENSO:

PHP:
 fb_like2() {
	if (is_single()):{ ?>
		<iframe src="http://www.facebook.com/plugins/like.php?href=<?php echo urlencode(get_permalink($post->ID)); ?>&amp;layout=standard&amp;show_faces=false&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:450px; height:60px;"></iframe>

		<?php endif;
}

function fb_like() {
	if (is_single() && !is_category( 8 )): ?>
		<iframe src="http://www.facebook.com/plugins/like.php?href=<?php echo urlencode(get_permalink($post->ID)); ?>&amp;layout=standard&amp;show_faces=false&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:450px; height:60px;"></iframe>

		<?php endif;
}

add_action('thesis_hook_after_post','fb_like');
add_action('thesis_hook_before_post','fb_like2');

De todas formas hacer ésto desde functions.php es más engorroso que editar simplemente el theme y hacer la función allí, que funciona seguro y sería más corta.

¡Un saludo!
 
Gracias por la ayuda! :encouragement:

La primera opción no funcionó y la segunda lo hizo, pero tuve que cambiarle esta línea:

Insertar CODE, HTML o PHP:
if (is_single() && !is_category( 8 )): ?>

por esta otra:

Insertar CODE, HTML o PHP:
if (is_single() && !in_category( '8' )) { ?>

Al final, el código lo puse en custom_functions.php (con el Custom File Editor de Thesis Theme) y quedó así:

Insertar CODE, HTML o PHP:
function fb_like2() {
	if (is_single()) { ?>
		<iframe src="http://www.facebook.com/plugins/like.php?href=<?php echo urlencode(get_permalink($post->ID)); ?>&amp;layout=standard&amp;show_faces=false&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:450px; height:60px;"></iframe>

		<?php }
}

function fb_like() {
 if (is_single() && !in_category( '8' )) { ?>
		<iframe src="http://www.facebook.com/plugins/like.php?href=<?php echo urlencode(get_permalink($post->ID)); ?>&amp;layout=standard&amp;show_faces=false&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:450px; height:60px;"></iframe>

		<?php }
}
add_action('thesis_hook_after_post','fb_like');
add_action('thesis_hook_before_post','fb_like2');

Mil gracias, Bizleet!
 
Cierto, no me di cuenta del detalle. Para eso estamos, suerte.
 

Temas similares

Atrás
Arriba