我已经为此工作了四个小时,但没有结果。有一个自定义分类法,我需要显示子类别的图像,但显示父类别的图像。
我的代码是:
<?php
$term = get_term_by( \'slug\', get_query_var( \'term\' ), get_query_var( \'taxonomy\' ) );
$queried_object = get_queried_object();
$term_id = get_queried_object() -> term_id;
$taxonomyName = "product-categories";
$termchildren = get_term_children( $term_id, $taxonomyName );
$image_id = get_term_meta( $term_id, \'showcase-taxonomy-image-id\', true );
if ($termchildren != false){
foreach ($termchildren as $child) {
$term2 = get_term_by( \'id\', $child, $taxonomyName );
?>
<div class="row col-xl-3 col-lg-3 col-md-3 col-sm-12 col-12 d-inline-block">
<img class="img-fluid imgborder" src="<?php echo wp_get_attachment_image_url( $image_id, \'\' ); ?>" alt="<?php echo $term2 -> name; ?>">
<h2><a href="<?php echo get_term_link($child, $taxonomyName); ?>"><?php echo $term2 -> name; ?></a></h2>
</div>
我知道问题出在这方面,但我不知道我到底应该怎么做。
$image_id = get_term_meta( $term_id, \'showcase-taxonomy-image-id\', true );
如果有人能帮助我,我将不胜感激。
SO网友:Daniel Fonda
还没有测试,但这应该可以。
<?php
$term = get_term_by( \'slug\', get_query_var( \'term\' ), get_query_var( \'taxonomy\' ) );
$queried_object = get_queried_object();
$term_id = get_queried_object() -> term_id;
$taxonomyName = "product-categories";
$termchildren = get_term_children( $term_id, $taxonomyName );
if (! empty ( $term_children ) ){
foreach ($termchildren as $child) {
$image_id = get_term_meta( $child, \'showcase-taxonomy-image-id\', true );?>
<div class="row col-xl-3 col-lg-3 col-md-3 col-sm-12 col-12 d-inline-block">
<img class="img-fluid imgborder" src="<?php echo wp_get_attachment_image_url( $image_id, \'\' ); ?>" alt="<?php echo $child->name; ?>">
<h2><a href="<?php echo get_term_link($child, $taxonomyName); ?>"><?php echo $child->name; ?></a></h2>
</div>
<?php }
?>
SO网友:Jacob Peattie
您只需移动$image_id
中的值foreach
循环,以便为每个子项检索图像。
下面是包含所做更改和其他一些更改的完整代码:
$queried_object = get_queried_object();
$term_children = get_term_children( $queried_object->term_id, $queried_object->taxonomy );
if ( ! empty ( $term_children ) ) :
foreach ( $term_children as $term_id ) :
$term = get_term_by( \'id\', $term_id, $queried_object->taxonomy );
$image_id = get_term_meta( $term_id, \'showcase-taxonomy-image-id\', true );
?>
<div class="row col-xl-3 col-lg-3 col-md-3 col-sm-12 col-12 d-inline-block">
<?php echo wp_get_attachment_image( $image_id, \'thumbnail\', false, array( \'class\' => \'img-fluid imgborder\' ) ); ?>
<h2><a href="<?php echo esc_url( get_term_link( $term_id ) ); ?>"><?php echo $term->name; ?></a></h2>
</div>
<?php
endforeach;
endif;
请注意我所做的其他一些更改:
我从一开始就去掉了不必要的值,只使用了get_queried_object()
获取当前术语的术语对象,并使用其属性获取术语子级我查过了$term_children
不是空的,而是false。get_term_children()
永远不会返回实际false
价值,所以! empty()
更准确地表示您正在检查的内容我设置$image_id
内部foreach
电流回路$term_id
对于儿童期我用过wp_get_attachment_image()
而不是wp_get_attachment_image_url()
因为我发现这比在HTML属性中混用PHP更简洁我用过esc_url()
在…上get_term_link()
为了额外的安全层,我还删除了get_term_link()
因为它不需要,而且可以缩短代码
SO网友:vesta
作品很棒。。。谢谢雅各布·皮蒂和丹尼尔·方达:)
<?php
$term = get_term_by( \'slug\', get_query_var( \'term\' ), get_query_var( \'taxonomy\' ) );
$queried_object = get_queried_object();
$term_id = get_queried_object() -> term_id;
$taxonomyName = "product-categories";
$termchildren = get_term_children( $term_id, $taxonomyName );
if ($termchildren != false){
foreach ($termchildren as $child) {
$term2 = get_term_by( \'id\', $child, $taxonomyName );
$image_id = get_term_meta( $child, \'showcase-taxonomy-image-id\', true );
?>
<div class="row col-xl-3 col-lg-3 col-md-3 col-sm-12 col-12 d-inline-block">
<img class="img-fluid imgborder" src="<?php echo wp_get_attachment_image_url( $image_id, \'\' ); ?>" alt="<?php echo $term2 -> name; ?>">
<h2 class="p-4"><a href="<?php echo get_term_link($child, $taxonomyName); ?>"><?php echo $term2 -> name; ?></a></h2>
</div>
SO网友:Narek Zakarian
而不是$term_id
, 你必须使用$termchildren
id并将其放入foreach循环中
if (! empty ( $term_children ) ){
foreach ($termchildren as $child) {
$image_id = get_term_meta( $child, \'showcase-taxonomy-image-id\', true );?>
<div class="row col-xl-3 col-lg-3 col-md-3 col-sm-12 col-12 d-inline-block">
<img class="img-fluid imgborder" src="<?php echo wp_get_attachment_image_url( $image_id, \'\' ); ?>" alt="<?php echo $child->name; ?>">
<h2><a href="<?php echo get_term_link($child, $taxonomyName); ?>"><?php echo $child->name; ?></a></h2>
</div>