我需要显示最新的8;
&
我已经轻松地在标题和href链接的FOREACHED。但我也想不出如何获得相关的图像,来创建一个类别瓷砖。
// Arguments
$args = array(
'taxonomy' => 'product_cat',
'posts_per_page' => 8,
'category_name' => $category->name,
'orderby' => 'date',
'order' => 'date'
);
$categories = get_categories( $args );
foreach( $categories as $category ) { ?>
<a href="<?php echo site_url(product-category) ?><?php echo $category->slug; ?>/">
<div class="category-tile-image">
<!-- I NEED THIS -->
<!-- I NEED THIS -->
<!-- I NEED THIS -->
<img src="#">
<!-- I NEED THIS -->
<!-- I NEED THIS -->
<!-- I NEED THIS -->
</div>
<h2><?php echo $category->name; ?></h2>
</a>
<?php } ?>
这是我想要展示的图像。
发布于 2021-11-16 23:19:30
为了输出每个类别图像,您需要在foreach
循环中做三件事。首先,您需要基于id of the category
获得id of the image
,然后基于该id,您将能够获得image url
。因此,在您的foreach
循环中,您可以这样做:
$image_id = get_term_meta($category->term_id, 'thumbnail_id', true);
$image_url = wp_get_attachment_image_url($image_id, 'thumbnail'); ?>
<img src="<?php echo $image_url; ?>">
所以你的整个代码都是这样的:
$args = array(
'taxonomy' => 'product_cat',
'posts_per_page' => 8,
'category_name' => $category->name,
'orderby' => 'date',
'order' => 'date'
);
$categories = get_categories($args);
foreach($categories as $category) {
$image_id = get_term_meta($category->term_id, 'thumbnail_id', true);
$image_url = wp_get_attachment_image_url($image_id, 'thumbnail');
?>
<a href="<?php echo $category->slug; ?>/">
<div class="category-tile-image">
<img src="<?php echo $image_url; ?>">
</div>
<h2><?php echo $category->name; ?></h2>
</a>
<?php }
结果如下:
你可以看到那些类别的图片有缩略图,而那些没有缩略图,它不会显示任何东西!
https://stackoverflow.com/questions/69996484
复制相似问题