Created
February 3, 2020 05:21
-
-
Save daler445/606ba8f300ec81790243bcbbd6edb1e1 to your computer and use it in GitHub Desktop.
List wordpress post categories and exclude categories by id
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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