Last active
August 29, 2023 10:18
-
-
Save hslaszlo/3bb7ef52ee424d60f083 to your computer and use it in GitHub Desktop.
Get first category name or id from wordpress post
This file contains 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 | |
// in the loop | |
$category = get_the_category(); | |
$currentcat = $category[0]->cat_ID; | |
$currentcatname = $category[0]->cat_name; | |
$currentcatslug = $category[0]->slug; | |
// outside the loop | |
global $post; | |
$categories = get_the_category($post->ID); | |
$currentcat = $category[0]->cat_ID; | |
$currentcatname = $category[0]->cat_name; | |
$currentcatslug = $category[0]->slug; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
// outside the loop
must change $category by $categories
Thanks!