0

使用自定义分类法构建页面,想在下面调用带有名称/标题/slug的图像,我的代码如上,谢谢帮助!

<?php

$types = get_terms( array(
    'taxonomy' => 'material',
    'hide_empty' => true,
) );

get_header();  ?>

//*html code*//

<?php foreach($types as $type) {
    $image = get_field('image', 'material_' . $type->term_id . '' );
    if ( has_term( $type->term_id, 'material')) {
        echo '<img src="' . $image . '" /> ';
    }
}?>
4

4 回答 4

1

这是工作代码

$prod_categories = get_terms( 'product_cat', array(
    'orderby'    => 'name',
    'order'      => 'ASC',
    'hide_empty' => true
));

foreach( $prod_categories as $prod_cat ) :
    $cat_thumb_id = get_woocommerce_term_meta( $prod_cat->term_id, 'thumbnail_id', true );
    $shop_catalog_img = wp_get_attachment_image_src( $cat_thumb_id, 'shop_catalog' );
    $term_link = get_term_link( $prod_cat, 'product_cat' );?>

    <a href="<?php echo $term_link; ?>"><img src="<?php echo $shop_catalog_img[0]; ?>" alt="<?php echo $prod_cat->name; ?>" /></a>

<?php endforeach; wp_reset_query();

谢谢

于 2020-02-06T07:16:56.867 回答
0

我以愚蠢的方式解决问题。如果有更好的方法,请告诉我。:)

<?php

$types = get_terms( array(
    'taxonomy' => 'material',
    'hide_empty' => true,
) );

get_header();  ?>

//*html code*//

<?php foreach($types as $type) {
    $image = get_field('image', 'material_' . $type->term_id . '' );
    if ( has_term( $type->term_id, 'material')) {
        echo '<img src="' . $image . '" /> ';
    }
    if ( has_term( $type->term_id, 'material')) {
        echo $type->name;
}
}?>
于 2020-02-06T06:57:03.123 回答
0

试试这个代码兄弟

$terms = get_terms( array(
    'taxonomy' => 'market-place',
    'hide_empty' => false,
    'orderby' => 'term_id',
    'order' => 'asc',
) );
foreach ($terms as $terms_row) {
    echo "<pre>";
    print_r($terms_row);
    echo "</pre>";
    $terms_row->slug;
}
于 2020-02-06T06:29:50.673 回答
0
    <?php

$types = get_terms( array(
    'taxonomy' => 'material',
    'hide_empty' => true,
) );

get_header();  ?>

//*html code*//

<?php foreach($types as $type) {
    $image = get_field('image', 'material_' . $type->term_id . '' );
    if ( has_term( $type->term_id, 'material')) {
        echo '<img src="' . $image['url'] . '" /> ';
    }
}?>
于 2020-02-06T02:00:07.163 回答