按产品类别获取 WooCommerce 产品永久链接
Get WooCommerce product permalink by product category
我编写了这段代码,变量 $link 没有返回产品 url,而是返回主页 url。请帮忙!我想要的是让产品 link 按类别选择它。显示就像按类别的菜单,但 link 直接转到我按类别选择的一种产品。
<ul>
<?php
$categories= get_terms( 'product_cat', array( 'parent' => 15, 'hide_empty' => false ) );
foreach($categories as $category): ?>
<li>
<div class="grid-subheader">
<?php
$metaArray = get_option('taxonomy_' . $category->term_id);
$productCatMetaTitle = $metaArray['wh_meta_title'];
$type = get_term_by( 'id', $category->parent, 'product_cat' );
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => 1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id', //This is optional, as it defaults to 'term_id'
'terms' => $category->term_id,
'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
)
)
);
$products = new WP_Query($args);
foreach ($products as $prod){
$link = get_the_permalink($prod->ID);
}
?>
<h3><?php echo $productCatMetaTitle; ?></h3>
<p><?php echo $category->description; ?></p>
<a href="<?php echo $link; ?>">
<div class="button-menu">
<?php //echo $type->name; ?> <?php echo $category->name; ?>
</div>
</a>
</div>
</li>
<?php endforeach; ?>
</ul>
尝试重新访问以下代码:
<ul>
<?php
$taxonomy = 'product_cat';
$category_terms = get_terms( $taxonomy, array( 'parent' => 15, 'hide_empty' => false ) );
foreach( $category_terms as $term ) { ?>
<li>
<div class="grid-subheader">
<?php
$meta = get_option('taxonomy_' . $term->term_id);
$title = isset($meta['wh_meta_title']) ? $meta['wh_meta_title'] : '';
$type = get_term_by( 'id', $term->parent, $taxonomy );
$query = get_posts( array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => 1,
'fields' => 'ids',
'tax_query' => array( array(
'taxonomy' => $taxonomy,
'field' => 'term_id',
'terms' => array( $term->term_id ),
) ),
) );
$product_id = reset($query);
?>
<h3><?php echo $title; ?></h3>
<?php if ( ! empty($term->description) ) ?>
<p><?php echo $term->description; ?></p>
<a href="<?php echo get_permalink($product_id); ?>">
<div class="button-menu"><?php echo $term->name; ?></div>
</a>
</div>
</li>
<?php
}
?>
</ul>
我编写了这段代码,变量 $link 没有返回产品 url,而是返回主页 url。请帮忙!我想要的是让产品 link 按类别选择它。显示就像按类别的菜单,但 link 直接转到我按类别选择的一种产品。
<ul>
<?php
$categories= get_terms( 'product_cat', array( 'parent' => 15, 'hide_empty' => false ) );
foreach($categories as $category): ?>
<li>
<div class="grid-subheader">
<?php
$metaArray = get_option('taxonomy_' . $category->term_id);
$productCatMetaTitle = $metaArray['wh_meta_title'];
$type = get_term_by( 'id', $category->parent, 'product_cat' );
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => 1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id', //This is optional, as it defaults to 'term_id'
'terms' => $category->term_id,
'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
)
)
);
$products = new WP_Query($args);
foreach ($products as $prod){
$link = get_the_permalink($prod->ID);
}
?>
<h3><?php echo $productCatMetaTitle; ?></h3>
<p><?php echo $category->description; ?></p>
<a href="<?php echo $link; ?>">
<div class="button-menu">
<?php //echo $type->name; ?> <?php echo $category->name; ?>
</div>
</a>
</div>
</li>
<?php endforeach; ?>
</ul>
尝试重新访问以下代码:
<ul>
<?php
$taxonomy = 'product_cat';
$category_terms = get_terms( $taxonomy, array( 'parent' => 15, 'hide_empty' => false ) );
foreach( $category_terms as $term ) { ?>
<li>
<div class="grid-subheader">
<?php
$meta = get_option('taxonomy_' . $term->term_id);
$title = isset($meta['wh_meta_title']) ? $meta['wh_meta_title'] : '';
$type = get_term_by( 'id', $term->parent, $taxonomy );
$query = get_posts( array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => 1,
'fields' => 'ids',
'tax_query' => array( array(
'taxonomy' => $taxonomy,
'field' => 'term_id',
'terms' => array( $term->term_id ),
) ),
) );
$product_id = reset($query);
?>
<h3><?php echo $title; ?></h3>
<?php if ( ! empty($term->description) ) ?>
<p><?php echo $term->description; ?></p>
<a href="<?php echo get_permalink($product_id); ?>">
<div class="button-menu"><?php echo $term->name; ?></div>
</a>
</div>
</li>
<?php
}
?>
</ul>