wordpressadvanced-custom-fields

Get the ACF text field value from the category of a custom post type in Wordpress


I am trying to create another class using the value of a text field from ACF PRO.

I have a custom post type named "portfolio" and it has a 4 categories, the ACF field setting is added to "taxonomy is equal to category".

When I edit a category I fill in the name that I want to get and then display like this :

<div class="grid-item-catalog catalog-project-Holder **the name need to be here**">

How do I get the ACF field value from the category?

Other information: my page template is page-portfolio.php and I am using an ACF repeater.

This is my code :

<!-- catalog -->
<div class="grid-catalog catalog-portfolio-wrraper">  
  <?php if ( $query->have_posts() ) {  while ( $query->have_posts() ) { $query->the_post(); ?>
     <!-- Repeater -->
     <?php if( have_rows('portfolio-projects-repeater') ){ ?>
        <?php while( have_rows('portfolio-projects-repeater') ) { the_row(); 
          // vars
          $projectDescription = get_sub_field('project-name');
          $projectImage = get_sub_field('project-image');
          $projectLinkText = get_sub_field('project-link-text');
          $projectLink = get_sub_field('project-link');                                                       
          ?> 
             <div class="grid-item-catalog catalog-project-Holder {the name need to be here}"> 
                  <div class="catalog-project-name"><?php the_title(); ?></div>
                  <div class="catalog-project-content-container">
                     <div class="catalog-project-image"><img src="<?php echo $projectImage['url']; ?>" alt="<?php echo $projectImage['alt'] ?>" /></div>
                     <div class="catalog-project-content">
                        <div class="description-link-container">
                             <div class="description"><?php echo $projectDescription; ?></div>
                             <div class="link"><a href="<?php echo $projectLink; ?>" target="_blank" rel="nofollow"><?php echo $projectLinkText; ?></a></div>
                        </div>
                     </div>
                  </div>
              </div>
        <?php } ?>
    <?php } ?>
    <!-- End Repeater --> 
    <?php } ?>
  <?php } ?>
</div>
<!-- end catalog -->

This is a screenshot of the settings for the category's ACF field, I hope it helps:

setup of the field


Solution

  • If I understand correctly, you have a custom post type "portfolio" and you want to get the ACF field value for the categories that the post belongs to. These categories are in the default Wordpress category (i.e. not a custom taxonomy). This is to do with the post and not the values in your repeater.

    If that is the case, you can get the value of the ACF field (category-name) for a category like this:

    $category_name = get_field('category-name', $category_id);
    

    This means we need to get the category ids for the post in your loop, and you can do this using get_the_category (assuming it is the default categories, for custom taxonomies you need get_the_terms). Also note that a post can have multiple categories so get_the_categories returns an array:

    $category_classes = "";
    // 1. GET THE CATEGORIES FOR THE CURRENT POST IN THE LOOP
    $categories = get_the_category( get_the_ID()); 
    if ( $categories ) {
        // 2. A POST CAN HAVE MULTIPLE CATEGORIES SO LOOP THROUGH THEM TO LOOK UP THE ACF FIELD
        foreach ( $categories as $category ) {
            // 3. ADD ALL ACF VALUE FOR THIS CATEGORY TO OUR $category_classes VARIABLE, SEPARATED BY SPACES
            $category_classes .= " ".get_field('category-name', $category->term_id);
        }        
    }
    

    Now $category_classes will have a space-separated list of the ACF values for the categories that you can use directly in your class attribute, i.e.

    <div class="grid-item-catalog catalog-project-Holder <?php echo $category_classes; ?>"> 
    

    Putting this into your code, it should be something like this:

    <!-- catalog -->
    <div class="grid-catalog catalog-portfolio-wrraper">  
    <?php if ( $query->have_posts() ) {  while ( $query->have_posts() ) { $query->the_post(); ?>
    
        <?php
        /* YOUR CODE TO GET THE POST CATEGORIES AND LOOK UP THE ACF FIELD VALUE */
        $category_classes = "";
        // 1. GET THE CATEGORIES FOR THE CURRENT POST IN THE LOOP
        $categories = get_the_category( get_the_ID()); 
        if ( $categories ) {
            // 2. A POST CAN HAVE MULTIPLE CATEGORIES SO WE NEED TO LOOP THROUGH THEM ALL
            foreach ( $categories as $category ) {
                // 3. ADD ALL ACF VALUE FOR THIS CATEGORY TO OUR $category_classes VARIABLE, SEPARATED BY SPACES
                $category_classes .= " ".get_field('category-name', $category->term_id);
            }        
        }
        ?>
    
      <!-- Repeater -->
      <?php if( have_rows('portfolio-projects-repeater') ){ ?>
        <?php while( have_rows('portfolio-projects-repeater') ) { the_row(); 
          // vars
          $projectDescription = get_sub_field('project-name');
          $projectImage = get_sub_field('project-image');
          $projectLinkText = get_sub_field('project-link-text');
          $projectLink = get_sub_field('project-link');                                                       
          ?> 
    
             <?php 4. OUTPUT THE CLASSES FOR THE DIV ?>
             <div class="grid-item-catalog catalog-project-Holder <?php echo $category_classes; ?>"> 
    
             <!--   the rest of your code here   -->
    
              </div>
          <?php } ?>
        <?php } ?>
        <!-- End Repeater --> 
        <?php } ?>
      <?php } ?>
    

    This code isn't tested, but it should at least point in you in the right direction :)