Skip to content

Instantly share code, notes, and snippets.

@daler445
Created February 3, 2020 05:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daler445/606ba8f300ec81790243bcbbd6edb1e1 to your computer and use it in GitHub Desktop.
Save daler445/606ba8f300ec81790243bcbbd6edb1e1 to your computer and use it in GitHub Desktop.
List wordpress post categories and exclude categories by id
<?php
/**
* Вывод категорий с исключением по id
* @param string $exclude - Список категорий разделенные с запятой
*
* Usage:
* exclude_post_categories("1,2,3")
*/
function exclude_post_categories($exclude = '') {
$categories = get_the_category($post->ID);
if (!empty($categories)) {
$categories_to_exclude = explode(",", $exclude);
$html = "";
$hasItem = false;
foreach($categories as $category) {
if (!in_array($category->cat_ID, $categories_to_exclude)) {
if ($hasItem == true) {
$html .= ", ";
}
$hasItem = true;
$html .= '<a href="' . get_category_link($category->cat_ID) . '" ';
$html .= 'title="' . $category->cat_name . '">' . $category->cat_name . '</a>';
}
}
echo $html;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment