Support

Account

Home Forums ACF PRO get value under woocommerce query

Solving

get value under woocommerce query

  • i create two filed for woocommerce category. and i want to do display those two those value on home page. can you guys help me out how to do that. i am not so expert on query writing .

    when place bellow code in home page template file it display woocommerce categories title with permalink. which is fine now i want with that show my filed value also

    <?php
    $args = array(
        'number'     => $number,
        'orderby'    => 'title',
        'order'      => 'ASC',
        'hide_empty' => $hide_empty,
        'include'    => $ids
    );
    $product_categories = get_terms( 'product_cat', $args );
    $count = count($product_categories);
    if ( $count > 0 ){
        foreach ( $product_categories as $product_category ) {
            echo '<h4><a href="' . get_term_link( $product_category ) . '">' . $product_category->name . '</a></h4>';
    
        }
    }
    
    ?> 

    one value will be like

      <?php if( get_field('cat_thumbnail') ): ?>
    
                        <img src="<?php the_field('cat_thumbnail'); ?>" />
    
                    <?php endif; ?>

    even guys if you think my code is correct or properly done or have more better way then no problem. all i need to display categories title with permalink and two filed values (one is text another is image)

  • You need to supply the $post_id that corresponds to the taxonomy term id like this

    
    <img src="<?php 
        the_field('cat_thumbnail', 'product_cat_'.$product_category->term_id);
         ?>" />
    
  • thanks for response but can you help me for full query please. i am not so expert on query writing

    i want loop then html template like

    <li><h4>category title</h4><div class="thmubnail"> custom filed image </div> <div class="shottxt">custome filed text</div></li>

  • I’m not exactly sure what you mean by query.

    Looking at your code, displaying the fields from the category would look something like this.

    
    <?php
      $args = array(
        'number'     => $number,
        'orderby'    => 'title',
        'order'      => 'ASC',
        'hide_empty' => $hide_empty,
        'include'    => $ids
      );
      $product_categories = get_terms( 'product_cat', $args );
      $count = count($product_categories);
      if ( $count > 0 ){
        foreach ( $product_categories as $product_category ) {
          echo '<h4><a href="' . get_term_link( $product_category ) . '">' . $product_category->name . '</a></h4>';
          if (get_field('text_field_name') ){
            ?>
              <p><?php the_field('text_field_name'); ?></p>
            <?php
          }
          if (get_field('cat_thumbnail') ){
            ?>
              <img src="<?php the_field('cat_thumbnail'); ?>" />
            <?php
          }
        }
      }
    ?>
    
  • thanks for reply and great supported. i tried your code and also tweak also some. but value not coming P and img tag coming empty if i remove if (get_field and if i add if (get_fieldthen tag also not come.

    i attached one jpg please check

  • You’re going to have to do a little work here. For example I did not know the name of the text field that you wanted to show so I used text_field_name, you’ll need to change that to the correct name.

    What is you image field set to return? ID, URL or Object? This would make a big difference on the code used to output the image.

  • This reply has been marked as private.
  • You still need to specify the IF for the term for acf to get the values.

    
    <?php
      $args = array(
        'number'     => $number,
        'orderby'    => 'title',
        'order'      => 'ASC',
        'hide_empty' => $hide_empty,
        'include'    => $ids
      );
      $product_categories = get_terms( 'product_cat', $args );
      $count = count($product_categories);
      if ( $count > 0 ){
        foreach ( $product_categories as $product_category ) {
          echo '<h4><a href="' . get_term_link( $product_category ) . '">' . $product_category->name . '</a></h4>';
          if (get_field('hexagon_text') ){
            ?>
              <p><?php the_field('hexagon_text', 
                  // specify correct post_id value for ACF
                    'product_cat_'.$product_category->term_id); ?></p>
            <?php
          }
          if (get_field('hexagon_thumbnail') ){
            ?>
              <img src="<?php the_field('hexagon_thumbnail', 
                  // specify correct post_id value for ACF
                    'product_cat_'.$product_category->term_id); ?>" />
            <?php
          }
        }
      }
    ?>
    
  • This reply has been marked as private.
  • I gave you the code above for getting the term id.

    For terms acf uses a constructed post id that is

    $taxonomy_name.'_'.$term_id

    when you get_terms(), you already know the name of the taxonomy. In you’re case, according to your code the taxonomy is product_cat. When you loop through the fields the term id is in $product_category->term_id

    So you can get the value of the field using
    the_field('field_name', 'product_cat_'.$product_category->term_id);

  • without change anything if i just copy and paste your code then only title coming. i checked html generate code also nothing there even

  • This reply has been marked as private.
Viewing 12 posts - 1 through 12 (of 12 total)

The topic ‘get value under woocommerce query’ is closed to new replies.