0

I have worked four hours on this but with no result. There is a custom taxonomy and I need to display the image of the child category but displays the image of parent category.

My code is:

<?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>

I know the problem is in this line, but I don't know what exactly I should do.

    $image_id = get_term_meta( $term_id, 'showcase-taxonomy-image-id', true );

I would appreciate if anyone can help me.

2

Haven't tested it, but this should do it.

    <?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 }
?>
| improve this answer | |
  • Thanks for the edit, thought it returned an object. Difficult without being able to var_dump(); – Daniel Fonda Nov 29 '17 at 15:28
0

You just need to move the setting of the $image_id value within the foreach loop so that the image is retrieved for each child term.

Here's complete code with that change made and a few others:

$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;

Note some of the other changes I made:

  1. I got rid of unnecessary values from the beginning and just used get_queried_object() to get the term object for the current term, and used its properties for getting the term children.
  2. I checked that $term_children wasn't empty, instead of false. get_term_children() will never return an actual false value, so ! empty() is a more accurate representation of what you're checking for.
  3. I set $image_id inside the foreach loop with the current $term_id for the child term.
  4. I used wp_get_attachment_image() instead of wp_get_attachment_image_url() because I find it neater than mixing up PHP in HTML attributes.
  5. I used esc_url() on get_term_link() for the extra layer of safety, and I also removed the taxonomy argument from get_term_link() because it's not needed and it keeps the code shorter.
| improve this answer | |
0

works wonderful... thank you Jacob Peattie and 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 ($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>
| improve this answer | |
-1

Instead of $term_id, you have to use $termchildren id and put it in foreach loop

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>

| improve this answer | |
  • Why did you vote down this answer? what is wrong? – Narek Zakarian Nov 29 '17 at 15:14
  • $termchildren is an Array of term IDs for the children of the given term. get_term_meta() requires an Integer for the ID of the one term that you want to get the metadata for. This code wouldn't do anything. – Jacob Peattie Nov 29 '17 at 15:18
  • I mean he have to use $termchildren ' s id and not parent term's id, that is clear to understand – Narek Zakarian Nov 29 '17 at 15:21
  • It's not at all clear to understand. It's the opposite. $termchildren is not the variable name in the asker's code, and is in fact the name of a different variable they're using, which is just confusing. And your answer doesn't account for the fact that the whole line of code is in the wrong place and needs to be moved. – Jacob Peattie Nov 29 '17 at 15:23
  • ok, you are right. I was uncareful – Narek Zakarian Nov 29 '17 at 15:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.