在主页上显示WooCommerce所有类别标题

时间:2016-02-18 作者:pagol007

如何在我的主题中显示所有woocommerce类别标题都是自定义字段值。

类似于下面的html示例

<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>
我尝试了很多方法,但仍然没有成功

<?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下面的代码只给了我类别标题和链接,但无法计算如何获得服装字段值

<?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
                )
            ),

        );

    }
}

?>
下面是获取自定义值代码的示例。我想把它放在里面

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

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

<?php endif; ?>

2 个回复
SO网友:Adam
<?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; ?>
SO网友:user101626

要在产品循环中获取ACF的值,需要传递产品的id,如果ACF在产品中,或者如果ACF在产品类别中,则id将是术语id,后跟product\\u cat\\u{term\\u id},作为\\u field或get field函数的第二个参数。