Answer the question
In order to leave comments, you need to log in
How to display the value of a certain variable in the open graph protocol?
How to display the value of a certain variable in the open graph protocol? Now when I share a page in social networks, it displays only og: title, og: url. I need to display next to, for example, the value of the variable, where does the information about the page ?
Answer the question
In order to leave comments, you need to log in
//Добавляем Open Graph в Language Attributes
function add_opengraph_doctype( $output ) {
return $output . ' xmlns:fb="http://ogp.me/ns/fb#" xmlns:og="http://ogp.me/ns#" ';
}
add_filter('language_attributes', 'add_opengraph_doctype');
//Разрешаем добавить Open Graph Meta Info в head
function insert_og_in_head() {
global $post;
setup_postdata( $post );
if ( !is_singular()) //if it is not a post or a page
return;
echo '<meta name="twitter:card" content="summary">'."n";
echo '<meta name="twitter:site" content="@mySite">'."n";
echo '<meta name="twitter:creator" content="@mySite">'."n";
echo '<meta name="twitter:title" content="' . get_the_title() . '"/>'."n";
echo '<meta name="twitter:url" content="' . get_permalink() . '"/>'."n";
echo '<meta name="twitter:description" content="'. strip_tags( get_the_excerpt($post->ID) ).'"/>'."n";
echo '<meta name="twitter:widgets:csp" content="on">'."n";
echo '<meta property="fb:admins" content="1000045454545"/>'."n";
echo '<meta property="og:title" content="' . get_the_title() . '"/>'."n";
echo '<meta property="og:type" content="article"/>'."n";
echo '<meta property="og:url" content="' . get_permalink() . '"/>'."n";
echo '<meta property="og:site_name" content="'. get_bloginfo('name') .'"/>'."n";
echo '<meta property="og:description" content="'. strip_tags( get_the_excerpt($post->ID) ).'"/>'."n";
if(!has_post_thumbnail( $post->ID )) { //у записи нет миниатюры - используем изображение по-умолчанию
$default_image="/img/logo.png";
echo '<meta property="og:image" content="' . $default_image . '"/>'."n";
echo '<meta name="twitter:image:src" content="' . $default_image . '"/>'."n";
}
else{
$thumbnail_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'large' );
echo '<meta property="og:image" content="' . esc_attr( $thumbnail_src[0] ) . '"/>'."n";
echo '<meta name="twitter:image:src" content="' . esc_attr( $thumbnail_src[0] ) . '"/>'."n";
}
}
add_action( 'wp_head', 'insert_og_in_head', 5 );
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question