Skip to content

Instantly share code, notes, and snippets.

@amorkovin
Last active March 10, 2021 13:22
Show Gist options
  • Save amorkovin/5365a205518fa65a30a261211211e874 to your computer and use it in GitHub Desktop.
Save amorkovin/5365a205518fa65a30a261211211e874 to your computer and use it in GitHub Desktop.
Все о рубриках WordPress
<?php
//Информация о рубрике поста:
$post_categorys_info = get_the_category();
$first_cat_post_id = $post_categorys_info[0]->cat_ID;
?>
<div class="reviews-item__rating"><a href="<?= get_category_link($first_cat_post_id) ?>"><?= get_cat_name($first_cat_post_id) ?></a></div>
<?php
//Вывод подрубрик на странице рубрики
$current_cat_id = get_query_var('cat');
$sub_categories = get_categories("child_of=$current_cat_id");
if ( count( $sub_categories ) ) {
?>
<div class="subcat">
<ul class="subcateg">
<?php foreach ($sub_categories as $sub_categorie) {
$sub_cat_name = $sub_categorie->cat_name;
$sub_cat_id = $sub_categorie->cat_ID;?>
<li class="cat-item"><a href="<?php echo get_category_link($sub_cat_id); ?> "><?php echo $sub_cat_name; ?></a></li>
<?php
}
?>
</ul>
</div>
<?php
}
//Получить информацию по рубрике на странице рубрики (объект с полной информацией о рубрике).
$cur_cat_id = get_query_var('cat');
$cat_info = get_category($cur_cat_id);
//Получение ID активной таксономии на странице таксономии. Также работает для рубрики.
$queried_object = get_queried_object();
$current_term_id = $queried_object->term_id;
//Получить ID родителя.
$cat_parent_id = $cat_info->parent;
//Получить дочерние рубрики.
$child_cats = get_categories("child_of=$cat_parent_id");
foreach ($child_cats as $item) {
$cats_name = $item->cat_name;
$cats_id = $item->cat_ID;
}
//Получить ссылку на рубрику
get_category_link($cat_id);
//Получить название рубрики
$cat_block_title = get_cat_name($cat_id);
// Получение ссылки на подрубрики на странице рубрики.
// В переменной $cur_cat хранится ID текущей рубрики.
<?php
$categories = get_categories("child_of=$cur_cat");
if ($categories) {
?>
<ul class="category-children">
<?php
foreach ($categories as $c_name) {
$cats_name = $c_name->cat_name;
$cats_id = $c_name->cat_ID; ?>
<li>
<a href="<?php echo get_category_link($cats_id); ?> "><?php echo $cats_name; ?></a>
</li>
<?php
} ?>
</ul>
<?php
}
//Еще вариант вывода ссылок на дочерние рубрики на странице категории
$categories = get_categories("child_of=$cur_cat");
if ($categories) {
?>
<div class="category-children">
<?php
foreach ($categories as $c_name) {
$cats_name = $c_name->cat_name;
$cats_id = $c_name->cat_ID; ?>
<div class="category-children__item">
<a href="<?php echo get_category_link($cats_id); ?> "><?php echo $cats_name; ?></a>
</div>
<?php
} ?>
</div>
<?php
}
//Проверка на нахождение в рубрике или ее подрубриках
if ( $cat_franchise_id && is_category($cat_franchise_id) || cat_is_ancestor_of($cat_franchise_id, $current_category) ) {
}
//Получить ID тега на странице тега или рубрики на странице рубрики
$tag_id = get_queried_object()->term_id;
//Вывести выпадающий список рубрик (или любой другой таксономии)
$args = array(
'class' => 'account-adding-user-content_post__category',
);
wp_dropdown_categories($args);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment