|
<?php #ВСТАВИТЬ В ФАЙЛ "archive.php" ?> |
|
|
|
<?php if (is_tag()): ?> |
|
<?php foreach (getCatsToTags() as $category) : ?> |
|
<?php $anchor = get_the_category_by_ID($category) ?> |
|
<a href="<?php echo esc_url(get_category_link($category)) ?>" title="<?php echo esc_html($anchor) ?>"><?php echo esc_html($anchor) ?></a> |
|
<?php endforeach; ?> |
|
<?php endif; ?> |
|
|
|
<?php #ВСТАВИТЬ В ФАЙЛ "functions.php" ?> |
|
|
|
<?php |
|
/** |
|
* @param int $slice |
|
* @param bool|false $disableCache |
|
* @return array|mixed |
|
*/ |
|
function getCatsToTags($slice = 10, $disableCache = false) |
|
{ |
|
$categories = []; |
|
if (!is_tag()) |
|
return $categories; |
|
|
|
$getCatsToTagsCount = 'getCatsToTagsCount' . get_query_var('tag_id'); |
|
$getCatsToTags = 'getCatsToTags' . get_query_var('tag_id'); |
|
|
|
if ($disableCache) { |
|
delete_transient($getCatsToTags); |
|
delete_transient($getCatsToTagsCount); |
|
} |
|
|
|
$posts = get_posts(['tag' => get_query_var('tag'), 'post_type' => 'post']); |
|
$categoriesItems = []; |
|
$postsCount = sizeof($posts); |
|
|
|
if (get_transient($getCatsToTagsCount) <> $postsCount) { |
|
foreach ($posts as $post) { |
|
foreach (get_the_category($post->ID) as $category) { |
|
$categoriesItems[$category->term_id][] = $category->term_id; |
|
} |
|
} |
|
|
|
array_multisort($categoriesItems, SORT_DESC); |
|
array_map(function ($item) use (&$categories) { |
|
$categories[] = $item[0]; |
|
}, array_slice($categoriesItems, 0, $slice)); |
|
|
|
set_transient($getCatsToTags, $categories, DAY_IN_SECONDS); |
|
set_transient($getCatsToTagsCount, $postsCount, DAY_IN_SECONDS); |
|
} else { |
|
$categories = get_transient($getCatsToTags); |
|
} |
|
|
|
wp_reset_postdata(); |
|
return $categories; |
|
} |