我建立了一个层次分类法。每个分类法都有一个图像字段,我试图在父页面上输出子分类法的标题、图像和链接。我已经成功地输出了标题和链接,但由于子分类图像是一个自定义字段,因此我一直在获取它。
我使用了下面的代码。
<?php
$term_id = get_queried_object()->term_id;
$taxonomy_name = get_query_var( \'taxonomy\' );
$termchildren = get_term_children( $term_id, $taxonomy_name );
$category_image = get_field(\'category_image\');
foreach ( $termchildren as $child ) {
echo \'<div class="col-sm-4 sub-caregory">\';
$term = get_term_by( \'id\', $child, $taxonomy_name );
echo \'<a href="\' . get_term_link( $child, $taxonomy_name ) . \'">\' . $term->name . \'</a><div class="sub-caregory-image"><img src="\' . $category_image[\'url\'] . \'" alt="\' . $category_image[\'alt\'] . \'" /></div>\';
wp_reset_postdata();
echo \'</div>\';
}
?>
最合适的回答,由SO网友:Seamus Leahy 整理而成
你需要问一下child term image field. 您只请求了当前页面的查询对象图像,根据查询对象的不同,该图像可能为空。
<?php
$term_id = get_queried_object()->term_id;
$taxonomy_name = get_query_var( \'taxonomy\' );
$termchildren = get_term_children( $term_id, $taxonomy_name );
// Loop through each child taxonomy term
foreach ( $termchildren as $child ) {
// Get the term object
$term = get_term_by( \'id\', $child, $taxonomy_name );
// Get the values we need to output
$url = get_term_link( $child, $taxonomy_name );
$name = $term->name;
// We make the call here to get the image and we pass in the term we want the image for
$image = get_field(\'category_image\', $term );
// Output it all
echo \'<div class="col-sm-4 sub-caregory">\';
echo \'<a href="\' . $url . \'">\' . $name . \'</a><div class="sub-caregory-image"><img src="\' . esc_url($image[\'url\']) . \'" alt="\' . esc_attr($image[\'alt\']) . \'" /></div>\';
echo \'</div>\';
}
?>