Last active
May 19, 2018 00:06
-
-
Save lillyfreitas/facb462d3a6ccf98f1a291880e3f0b49 to your computer and use it in GitHub Desktop.
Adicionar manualmente tags Open Graph em tema do WordPress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Compartilhamento em redes sociais --> | |
<!-- OPEN GRAPH --> | |
<?php | |
$img_compartilhe = get_post_meta($post->ID,'_thumbnail_id',false); | |
$img_compartilhe = wp_get_attachment_image_src($img_compartilhe[0], false); | |
$img_compartilhe = $img_compartilhe[0]; | |
$img_padrao = get_bloginfo('stylesheet_directory').'/SUAIMAGEM.jpg'; | |
// Recupera a URL da categoria | |
$the_cat = get_the_category(); | |
$category_name = $the_cat[0]->cat_name; | |
$category_link = get_category_link( $the_cat[0]->cat_ID ); | |
?> | |
<meta content="pt_br" property="og:locale"> | |
<meta name="twitter:card" content="summary_large_image" /> | |
<meta name="twitter:site" content="@SEUPERFILNOTWITTER" /> | |
<meta property="og:type" content="<?php if ( is_single() || is_page() ) { echo "article"; } else { echo "website"; } ?>"> | |
<meta property="og:site_name" content="<?php bloginfo('name'); ?>"> | |
<meta property="og:url" content="<?php if ( is_home() ) { echo bloginfo('url');} | |
elseif ( is_category() ) { echo $category_link; } else { echo the_permalink(); } ?>"/> | |
<meta name="twitter:title" property="og:title" content="<?php if ( is_home() ) { echo bloginfo('name'); } | |
elseif ( is_page( 2 )) { echo "NOME DO SEU SITE OU EMPRESA"; } | |
elseif ( is_category() ) { echo wp_title('|', true, 'right'), bloginfo('name'); } | |
else { echo single_post_title(''); } ?> " /> | |
<meta name="twitter:description" property="og:description" content="<?php | |
if ( is_home() ) { echo bloginfo('description'); } | |
elseif ( is_page( 2 )) { echo "DESCRIÇÃO DO SEU SITE OU EMPRESA"; } | |
else { while(have_posts()):the_post(); | |
echo $excerpt = strip_tags( get_the_excerpt() ); | |
//echo substr($excerpt,0, 300); | |
//if (strlen($excerpt) > 299){ echo '...';} | |
endwhile; } | |
?>"/> | |
<meta name="twitter:image" property="og:image" content="<?php if ( is_home() || is_category() ) { echo $img_padrao; } | |
else { if ( $img_compartilhe[0] == null ) { echo $img_padrao; } else { echo $img_compartilhe; } } ?>" /> | |
<meta property="og:image:type" content="image/png"> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Edite o código do header.php do tema FILHO(*) do WordPress:
Adicionar na tag
<html>
os atributosxmlns:og
exmlns:fb
:<html <?php language_attributes(); ?> xmlns="http://www.w3.org/1999/xhtml" xmlns:og="http://ogp.me/ns#" xmlns:fb="https://www.facebook.com/2008/fbml">
Copie o código acima e cole antes do fim da tag
<head>
do arquivo header.php do seu tema filhoAltere os itens em maiúscula para os valores de seu site:
(*) Não edite diretamente no tema original, se não for de sua autoria. Saiba mais sobre temas filho no WordPress.
Observação: este código não é originalmente de minha autoria, embora eu tenha feito ajustes, mas no momento não encontro a fonte :(