Advertisement
pusatdata

How Show Slug Category

Jul 12th, 2017
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. For category view you can use this code snippet.
  2. <?php
  3. if(is_category()) {
  4. $category = get_query_var('cat');
  5. $current_cat = get_category($cat);
  6. echo 'The slug is ' . $current_cat->slug;
  7. }
  8. ?>
  9.  
  10. ==============
  11.  
  12. For single view or post view
  13. <?php
  14. $terms = get_the_terms( $post->ID , 'category');
  15. if($terms) {
  16. foreach( $terms as $term ) {
  17. $cat_obj = get_term($term->term_id, 'category');
  18. $cat_slug = $cat_obj->slug;
  19. }
  20. }
  21. echo 'The slug is '. $cat_slug;
  22. ?>
  23.  
  24. SIMPEL:
  25. <?php $terms = get_the_terms( $post->ID , 'category'); if($terms) { foreach( $terms as $term ) { $cat_obj = get_term($term->term_id, 'category'); $cat_slug = $cat_obj->slug; } } echo $cat_slug; ?>
  26.  
  27.  
  28. ==============
  29. Still hungry?
  30. If in the case you only want to get category slug using category ID don’t worry folks, I’ve provided a solution on it too.
  31.  
  32. Step 1:
  33. In your function.php file, put this function.
  34.  
  35. function get_cat_slug($cat_id) {
  36. $cat_id = (int)$cat_id;
  37. $category = &get_category($cat_id);
  38. return $category->slug;
  39. }
  40.  
  41.  
  42. Step 2:
  43. Once done, you can use the function like the below code.
  44. <?php
  45. echo get_cat_slug(1);
  46. // Where 1 is the category ID, this code will display the slug of the category ID 1.
  47. ?>
  48.  
  49. atau
  50. <?php echo get_cat_slug(); ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement