plug/s1-show-category-description/s1-show-category-description.php

102 lines
3.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/*
Plugin Name: S1 show category description
Plugin URI: https://1studi.ru
Description: Выводит описание категории/рубрики на странице архива (по умолчанию WP его скрывает)
Version: 1.1.0
Author: S1
Author URI: https://1studi.ru
License: GPL-2.0+
Text Domain: s1-custom-scroll-to-top
Domain Path: /languages
*/
if (!defined('ABSPATH')) {
exit;
}
add_action('wp_head', function() {
// Можно добавить стили прямо в head (или подключить отдельный css)
// Раскомментируйте, если нужно
/*
echo '<style>
.category-description {
margin: 1.5em 0 2.5em;
padding: 1.2em 1.8em;
background: #f8f9fa;
border-left: 4px solid #0073aa;
border-radius: 4px;
line-height: 1.6;
}
.category-description p:last-child {
margin-bottom: 0;
}
</style>';
*/
}, 20);
/**
* Основной вывод описания
*/
function scd_show_category_description() {
// Показываем только на страницах архивов категорий (можно расширить)
if (!is_category() && !is_tag() && !is_tax()) {
return;
}
// Получаем описание
$description = get_the_archive_description();
if (empty($description)) {
return;
}
// Можно обернуть в свой класс или изменить тег
$output = '<div class="category-description entry-content">';
$output .= $description;
$output .= '</div>';
echo $output;
}
/**
* Вариант А — после заголовка архива (самый популярный)
*/
add_action('woocommerce_archive_description', 'scd_show_category_description', 15); // для woocommerce
add_action('generate_archive_title', 'scd_show_category_description', 20); // GeneratePress
add_action('astra_archive_header', 'scd_show_category_description', 15); // Astra
// Универсальный способ — если тема не использует специальные хуки
add_action('wp_head', function() {
if (is_category() || is_tag() || is_tax()) {
add_filter('the_content', 'scd_inject_description_to_content', 9);
}
});
function scd_inject_description_to_content($content) {
// Показываем только в начале основного контента архива
if (!is_main_query() || !in_the_loop()) {
return $content;
}
remove_filter('the_content', 'scd_inject_description_to_content', 9);
$description = get_the_archive_description();
if (empty($description)) {
return $content;
}
return '<div class="category-description">' . $description . '</div>' . $content;
}
/**
* Вариант Б — короткий код [category_description]
* Можно вставить в любой блок / виджет / шаблон
*/
add_shortcode('category_description', function() {
return get_the_archive_description();
});