Support

Account

Home Forums Front-end Issues Custom Taxonomy in ACF

Solved

Custom Taxonomy in ACF

  • Hi!

    I have a custom taxonomy named “Departments” and I’m having an advanced custom field as “Sub Specialty” (sub_specialty)… Instead of manually inputting the departments from the taxonomy Departments, I used the
    Relationship > Taxonomy…

    It’s working fine.. It’s just that, the output it displays is the ID of the item(s) instead of the term/value/name.. Here’s my code on the page template (Sub Specialty section):

    <?php 
    						/* sub specialty */
    						if (get_field('sub_specialty') != '') { ?>
    						<strong>Subspecialty.:</strong>
    						<?php the_field('sub_specialty'); ?>
    						<br />
    						<?php } else { ?>
    						
    						<?php } ?>

    I’ve tried the code used on these following links:
    http://support.advancedcustomfields.com/forums/topic/array-wp_error-trying-to-get-acf-taxonomy-value/
    http://support.advancedcustomfields.com/forums/topic/how-to-get-values-from-a-custom-taxonomy/

    but those did not worked… I’m pretty new in PHP.. Someone please help~

    Thanks!

  • Hi!

    Do you mean that departments are a custom post type?

    I dont understand how the setup is.. 🙂

  • Hi! The custom post type is “dims”.. the Departments is a custom taxonomy… =]

    Here is how I want it to be:
    I have a custom post type “Doctors”, each “Doctor” are assigned in different “Department” and has “Sub-Specialty/Specialties”..

    The Sub-Specialty should have the same list as of the Department, so I used the Relationship > Taxonomy of ACF to call the list of “Departments”. It’s working on the back-end, but on the front-end, what being displayed is the Item ID of the “Department” not the Item Name/Value.

    The taxonomy “Departments” is in checbox format.. =]

    Thanks!

  • ah..

    On the field in admin you can select wether you want it to return the term ID or the term object.. Switch it over to term object then do this where you want the name:

    
    <?php echo $term->name; ?>
    

    This is based on that in your foreach loop you call the single term $term. So just switch that out if you use something else 🙂

  • Hi Jonathan!

    I switched the return value to Term Object as your reco but the code did not work. Anyways, I fixed it already and here’s my working code:

    <?php 
    						/* sub specialty */
    						if (get_field('sub_specialty') != '') { ?>
    						<strong>Sub specialty.:</strong>
    						<?php
    $terms = get_field('sub_specialty');
    array($terms);
    foreach ($terms as $term){
    $subspec[] = '<a href="'.$term->slug.'" name="'.$term->slug.'" alt="'.$term->anem.'">'. $term->name .'</a>';
    }
    echo implode(' / ', $subspec);
    ; ?>
    						<br />
    						<?php } else { ?>
    						
    						<?php } ?>

    I added some codes too according to how I want it to be displayed.

    Thanks for replying btw. =]

    God bless!! ;]

  • Hi! Okay well I don’t know how your code looked before that was just to show you how to retrieve the name rather than just the id 🙂

    Your code seems a bit excessive and unsemantic. Is there any reason it couldn’t look like this?

    
    
    <?php if (get_field('sub_specialty')) { //Sub speciality ?>
    	<strong>Sub specialty.:</strong>
    	<ul>
    	<?php $terms = get_field('sub_specialty'); ?>
    	<?php foreach ($terms as $term){ ?>
    			<li><a href="<?php echo $term->slug; ?>" name="<?php $term->slug; ?>" alt="<?php $term->name; ?>"><?php echo $term->name; ?></a></li>
    	<?php } ?>
    	</ul>
    <?php } ?>
    

    There’s no unnecessary functions running, the else-statement was removed, the if statement is changed to be simpler, name was corrected and the html is semantically correct since it’s now an unordered list instead of links with an inline break. 🙂

    Happy coding

  • Hi!

    Well, it shouldn’t be listed as a “list” as in

      or

        .. I want it listed in one line without touching the css so I coded it that way.. Also, there is a possibility that not all the Doctors will have a “Sub Specialty” that’s why there’s this line of code:

        <?php 
        if (get_field('sub_specialty') != '') { ?>

        I removed the array($terms); too. =]

        Here’s my final code on displaying the term name:

        <?php
            $terms = get_field('sub_specialty');
            foreach ($terms as $term){
                $subspec[] = '<a href="'.$term->slug.'" name="'.$term->slug.'" alt="'.$term->anem.'">'. $term->name .'</a>';
             }
            echo implode(' / ', $subspec);
        ?>
      1. Thank you for the suggestion anyway. I’m pretty new in PHP so my code might really seem unsemantic or such. Will work on that ;]

        Thanks! =]

      2. Alright 🙂

        Altho this:

        
        <?php 
        if (get_field('sub_specialty') != '') { ?>
        

        says “do stuff if this field is not an empty string”

        while this (in my code):

        
        <?php if (get_field('sub_specialty')) { //Sub speciality ?>
        

        says “do stuff if this field has any values”. So you should still use that instead 🙂 Its pretty much a shorthand.. say if you want to apply something when there’s no field it could say this:

        
        <?php if (!get_field('sub_specialty')) { //Sub speciality ?>
        
      3. ah.. I see..

        I’ll use yours then.. =]

        Thanks for the advise! ^_^

      Viewing 10 posts - 1 through 10 (of 10 total)

      The topic ‘Custom Taxonomy in ACF’ is closed to new replies.