add opengraph
This commit is contained in:
@@ -245,6 +245,9 @@ function add_category_class_to_posts($classes, $class, $post_id) {
|
|||||||
add_filter('post_class', 'add_category_class_to_posts', 10, 3);
|
add_filter('post_class', 'add_category_class_to_posts', 10, 3);
|
||||||
|
|
||||||
|
|
||||||
|
require_once 'inc/opengraph.php';
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
80
inc/opengraph.php
Normal file
80
inc/opengraph.php
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
<?php
|
||||||
|
function custom_open_graph_tags() {
|
||||||
|
// Дефолтная картинка
|
||||||
|
$default_image = 'https://sportpressa.ru/wp-content/uploads/2026/01/sportpressa-ru.png';
|
||||||
|
|
||||||
|
// Для постов
|
||||||
|
if (is_single()) {
|
||||||
|
$title = get_the_title();
|
||||||
|
$description = wp_trim_words(get_the_excerpt(), 30);
|
||||||
|
$url = get_permalink();
|
||||||
|
$type = 'article';
|
||||||
|
|
||||||
|
// Картинка поста или дефолтная
|
||||||
|
if (has_post_thumbnail()) {
|
||||||
|
$image = get_the_post_thumbnail_url(get_the_ID(), 'full');
|
||||||
|
} else {
|
||||||
|
$image = $default_image;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Для главной и архивов (категории, теги)
|
||||||
|
elseif (is_home() || is_archive()) {
|
||||||
|
$type = 'website';
|
||||||
|
$url = get_permalink();
|
||||||
|
$image = $default_image; // Всегда дефолтная картинка
|
||||||
|
|
||||||
|
if (is_home()) {
|
||||||
|
$title = get_bloginfo('name');
|
||||||
|
$description = get_bloginfo('description');
|
||||||
|
} elseif (is_category()) {
|
||||||
|
$title = single_cat_title('', false);
|
||||||
|
$description = category_description() ? wp_trim_words(category_description(), 30) : get_bloginfo('description');
|
||||||
|
} elseif (is_tag()) {
|
||||||
|
$title = single_tag_title('', false);
|
||||||
|
$description = tag_description() ? wp_trim_words(tag_description(), 30) : get_bloginfo('description');
|
||||||
|
} else {
|
||||||
|
$title = get_the_archive_title();
|
||||||
|
$description = get_bloginfo('description');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Для страниц
|
||||||
|
elseif (is_page()) {
|
||||||
|
$title = get_the_title();
|
||||||
|
$description = wp_trim_words(get_the_excerpt(), 30);
|
||||||
|
$url = get_permalink();
|
||||||
|
$type = 'website';
|
||||||
|
|
||||||
|
if (has_post_thumbnail()) {
|
||||||
|
$image = get_the_post_thumbnail_url(get_the_ID(), 'full');
|
||||||
|
} else {
|
||||||
|
$image = $default_image;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return; // Не выводим OG теги на других страницах
|
||||||
|
}
|
||||||
|
|
||||||
|
// Вывод мета-тегов
|
||||||
|
?>
|
||||||
|
<meta property="og:title" content="<?php echo esc_attr($title); ?>" />
|
||||||
|
<meta property="og:description" content="<?php echo esc_attr($description); ?>" />
|
||||||
|
<meta property="og:type" content="<?php echo esc_attr($type); ?>" />
|
||||||
|
<meta property="og:url" content="<?php echo esc_url($url); ?>" />
|
||||||
|
<meta property="og:site_name" content="<?php echo esc_attr(get_bloginfo('name')); ?>" />
|
||||||
|
<meta property="og:image" content="<?php echo esc_url($image); ?>" />
|
||||||
|
<meta property="og:locale" content="ru_RU" />
|
||||||
|
|
||||||
|
<?php if (is_single()) : ?>
|
||||||
|
<meta property="article:published_time" content="<?php echo get_the_date('c'); ?>" />
|
||||||
|
<meta property="article:modified_time" content="<?php echo get_the_modified_date('c'); ?>" />
|
||||||
|
<?php endif; ?>
|
||||||
|
|
||||||
|
<!-- Twitter Cards -->
|
||||||
|
<meta name="twitter:card" content="summary_large_image" />
|
||||||
|
<meta name="twitter:title" content="<?php echo esc_attr($title); ?>" />
|
||||||
|
<meta name="twitter:description" content="<?php echo esc_attr($description); ?>" />
|
||||||
|
<meta name="twitter:image" content="<?php echo esc_url($image); ?>" />
|
||||||
|
<?php
|
||||||
|
}
|
||||||
|
add_action('wp_head', 'custom_open_graph_tags', 5);
|
||||||
|
?>
|
||||||
Reference in New Issue
Block a user