O
O
orangestudio2018-08-18 22:13:18
WordPress
orangestudio, 2018-08-18 22:13:18

Woocommerce and ACF. In an arbitrary place, I displayed the WC categories and I can not determine the link for the categories. How to insert a link in a category?

Woocommerce and ACF. In an arbitrary place, I deduced WC categories, for WC categories through ACF I defined an arbitrary field - a link. Here is the category code:

add_action( 'homepage', 'mason', 44 );
function mason(   ) { ?>

<?php
                            $args = array(
                                'taxonomy' => 'product_cat',
                                'orderby' => 'tax_position',
                'meta_key' => 'tax_position',
                                'order'      => 'ASC',
                                'hide_empty' => true
                            );

                            $product_categories = get_terms( $args );
                            
                            $count = count($product_categories);
                          
                            if ( $count > 0 ){
                                foreach ( $product_categories as $product_category ) {
                                    $thumbnail_id = get_woocommerce_term_meta( $product_category->term_id, 'thumbnail_id', true );
                  
                  
                                    $item = '<div class="catalogue-item">';
                  $item .= '<a href="';
                  
                  $item .= '" class="catalogue-item-link">';
                                    $item .= '<div class="catalogue-item-img tilt">';
                                    $item .= '<img src="'.  wp_get_attachment_url( $thumbnail_id ) .'" alt="" >';
                                    $item .= '</div>';
                                    $item .= '<p class="catalog-item-title">' . $product_category->name . '</p>';
                                    $item .= '</div></a>';
                  
                                    echo $item;  
                              
                                }
                            }
                            ?>

<?php
}

Where $item a href... I can't insert a link because it is obtained via ' . the_field( 'url_cat', $product_category ) .'
If you just write the text, then it appears in a href="ssilka". And I insert this code, the link appears but outside a href and just like text. Thanks

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2018-08-19
@orangestudio

try to put the value of an arbitrary field into a variable and display it where necessary

<?php
  $args = array(
    'taxonomy' => 'product_cat',
    'orderby' => 'tax_position',
    'meta_key' => 'tax_position',
    'order'      => 'ASC',
    'hide_empty' => true
  );

  $product_categories = get_terms( $args );

  $count = count($product_categories);

  if ( $count > 0 ){
    foreach ( $product_categories as $product_category ) {
      $link = get_field( 'url_cat', $product_category );
      $thumbnail_id = get_woocommerce_term_meta( $product_category->term_id, 'thumbnail_id', true );


      $item = '<div class="catalogue-item">';
      $item .= '<a href="'.$link.'"class="catalogue-item-link"'>;

      $item .= '<div class="catalogue-item-img tilt">';
      $item .= '<img src="'.  wp_get_attachment_url( $thumbnail_id ) .'" alt="" >';
      $item .= '</div>';
      $item .= '<p class="catalog-item-title">' . $product_category->name . '</p>';
      $item .= '</div></a>';

      echo $item;  

    }
  }
  ?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question