2

how can i display in my theme all woocommerce category title are custom filed value.

example html like bellow

<li><a href="#">
          <div>
            <h2>category title</h5>
            <h3>custom field one value</h6>
          </div>
          <div class="imgpos">custom field two value</div>
          </a></li>

i tried many way but still not success

<?php
$post_type = 'product';
// Get all the taxonomies for this post type
$taxonomies = get_object_taxonomies( (object) array( 'post_type' => $post_type ) );
foreach( $taxonomies as $taxonomy ) : 
    // Gets every "category" (term) in this taxonomy to get the respective posts
    $terms = get_terms( $taxonomy );
    foreach( $terms as $term ) : 
        $posts = new WP_Query( "taxonomy=$taxonomy&term=$term->slug&posts_per_page=2" );
        if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post();
          ?>
          <a href="<?php the_permalink(); ?>"><span><?php the_title(); ?></span></a><br><br>
          <?php
        endwhile; endif;
    endforeach;
endforeach;
?>

UPDATE 1 bellow code only give me category title and link but can not figure out how to get costume 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>';
        $args = array(
            'posts_per_page' => -1,
            'tax_query' => array(
                'relation' => 'AND',
                array(
                    'taxonomy' => 'product_cat',
                    'field' => 'slug',
                    // 'terms' => 'white-wines'
                    'terms' => $product_category->slug
                )
            ),

        );

    }
}

?>

example for get custom value code will be bellow. i want fit that inside div

 <?php if( get_field('hexagon_thumbnail') ): ?>

    <img src="<?php the_field('hexagon_thumbnail'); ?>" />

<?php endif; ?>
  • Are you trying to display a list of categories or a list of products with the categories and meta data in which are attached to the products? – Adam Feb 18 '16 at 6:34
  • thanks for comment. i want to display list of categories . Title and two custom filed value. i used AFC so code will be like <?php the_field('custom_text'); ?> and <?php $image = get_field('image'); if( !empty($image) ): ?> <img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" /> <?php endif; ?> – pagol007 Feb 18 '16 at 7:12
  • 1
    @pagol007 Please file an edit and add the info in your comment above directly inside your question. Code in comments are unreadable, also, valueble info that can help others in answering your question should be inside your question, not a useless comments ;-) – Pieter Goosen Feb 18 '16 at 7:45
1
<?php

$post_type = 'product';

$taxonomies = get_object_taxonomies((object) array( 'post_type' => $post_type ));

foreach ($taxonomies as $taxonomy) : 

    $terms = get_terms($taxonomy);

    foreach ($terms as $term) : 

        $term_link = get_term_link($term->term_id);

        $posts = new WP_Query( "taxonomy=$taxonomy&term=$term->slug&posts_per_page=2" ); ?>

        <li>

            <h2>
                <a href="<?php echo $term_link; ?>"><?php echo $term->name; ?></a>
            </h2>

            <?php 

            if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post(); ?>

                <div>

                <?php if( get_field('hexagon_thumbnail') ): ?>

                    <img src="<?php the_field('hexagon_thumbnail'); ?>" />

                <?php endif; ?>

                </div>

            <?php endwhile; endif; ?>

        </li> <!-- end list item -->

<?php endforeach; endforeach; ?>
  • thanks for your great help. actually now showing category with two other post. and post link i want only woocommcer category and category link and costume filed value. costume value not coming now. only coming category title. permalink coming from product post. i only want category name. with category links and custom filed value i think my query having problem. – pagol007 Feb 18 '16 at 9:15
  • i got one code. which is bit work. you can check. i just updated my post – pagol007 Feb 18 '16 at 9:46
  • I made an edit to the example above... try that. – Adam Feb 18 '16 at 9:47
  • nah it is result same. only title come also 2 post come. too. i think this query $posts = new WP_Query( "taxonomy=$taxonomy&term=$term->slug&posts_per_page=2" ); .. page post per page. i dont want post per page. i want only category title. i updated new code that is fine disply only category with permalink but no idea inside there how i can get custom filed value – pagol007 Feb 18 '16 at 9:58
  • Your code is quite expensive to run and hits the db quite hard ;-) – Pieter Goosen Feb 18 '16 at 10:50
0

To fetch the value of ACF in the product loop you need to pass the id of the product, if the acf in product, or if the ACF in product category the id will be term id followed by product_cat_{term_id} as the second parameter of the the_field or get field function.

  • Where is this ACF coming from, OP didn't mention about it. – bravokeyl Aug 25 '16 at 7:40
  • @bravokeyl The OP is using get_field(), very likely from ACF. – fuxia Aug 25 '16 at 8:30
  • @toscho got it. – bravokeyl Aug 25 '16 at 8:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.