Skip to content

Instantly share code, notes, and snippets.

@Spellhammer
Created November 13, 2020 17:00
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Spellhammer/758135281eafee8d29ef05aac585c929 to your computer and use it in GitHub Desktop.
Save Spellhammer/758135281eafee8d29ef05aac585c929 to your computer and use it in GitHub Desktop.
isotope.category.codeblock.php
<?php
$categories = get_the_category(get_the_ID());
foreach( $categories as $category ) {
echo "<span class='category'>$category->slug</span>";
}
?>
@drjuanc
Copy link

drjuanc commented Jul 7, 2021

Hi there.
What if I´m using a custom post type, with custom taxonomies?

In that case, get_the_category doesn´t work.

But you can use get_the_terms function instead.

$category->slug"; } ?>

@joffreypersia
Copy link

joffreypersia commented Nov 18, 2021

I tried :

<?php
	$categories = get_the_terms(get_the_ID(), 'my_acf_field');

	foreach( $categories as $category ) {
     
      echo "<span class='category'>$category->slug</span>";

  }
?>

But it doesn't work.

@irineltraista
Copy link

irineltraista commented Feb 16, 2022

You can try this if you want to use it with custom post types.
Just replace 'my_taxonomy' with your taxonomy.

<?php   // Get terms for post

$terms = get_the_terms( $post->ID , 'my_taxonomy' );
 // Loop over each item since it's an array
 if ( $terms != null ){
 foreach( $terms as $term ) {

 echo "<span class='category'>$term->slug</span>";
 // Get rid of the other data stored in the object, since it's not needed
 unset($term);
} } 
?>

@MarkHuyton
Copy link

Hi
I have a taxonmy 'location' where the user can select a location where the live, i put the code above in the repeater code block replacing 'my_taxonomy' with 'location' I then have 3 buttons with different locations, what code is used the the main code block?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment