0
votes

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

4
I don't understand what field you are trying to get. Can you show us a screenshot of your ACF setup for the field?FluffyKitten
i add a screenshot as you ask, i hope it is more clear now :-)GDdesign

4 Answers

0
votes

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 :)

0
votes

In your code you are saving a custom field with the name project-name in a variable:

$projectDescription = get_sub_field('project-name');

Maybe you should change this meta key of the subfield to "project-description", it might be confusing, if you also have the name of a project.

To output a value in php you use <?php echo $variableName; ?>

You are accessing the values in a repeater called portfolio-projects-repeater. So your fields are inside of this and need to be accessed using a while, like you did.

Your screenshot shows a text field, which is not inside a repeater field.

So if you want to get the value of that field, you just need to use get_field(). This do not need to be in a while and not be called by using get_sub_field.

Your code therefore should look like:

    $variableName = get_field('category-name');

After saving to variable, you can simply output it with:

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

Yaaaayyy... I succeeded, with this code:

         <?php
            // load all 'category' terms for the post
            $terms = get_the_terms( get_the_ID(), 'category');
            // we will use the first term to load ACF data from
            if( !empty($terms) ) {
                $term = array_pop($terms);
                $custom_field = get_field('category-name', $term );
            }
          ?>  

In any case i want to thank you for all yours reply's , i am very appreciat this.

0
votes

Create A Template in the theme and then use the below code, Just change the slug (banner_image)

<?php
    //Get the Post ID
    $id = get_the_ID();
?>

    <img src="<?php echo get_field('banner_image', $id); ?>" />
    <div class="col-md-4 text-center"><?php echo get_field( "banner_heading", $id); ?></div>