简体   繁体   中英

Get images from custom taxonomy terms

Ok, I don't know if asking a new question is allowed for this. But I think its a bit different than my previous question. (if not, please tell me).

Im trying to get featured images from my custom taxonomy terms. Yesterday I got pretty far, but my client wants to add the names of the team members. So I'm kind of back to the drawing board on this one.

My theme is made up out of four different collapsible panels. With a Custom Post type and a loop.

So I made a custom taxonomy ons_team because I do not want to show the team members on every panel. So I checked the boxes on the ctp where I want the team members to be shown.

But now that the client wants to add the names of the team members I'm forced to use a different code. Yesterday I got it to work with this code.

<?php $terms = get_the_terms( $post->ID , 'ons_team' ); 
                    print apply_filters( 'taxonomy-images-list-the-terms', '', array(
                                'before'       => '<div class="ons-team">',
                                'after'        => '</div>',
                                'before_image' => '<span>',
                                'after_image'  => '</span>',
                                'image_size'   => 'detail',
                                'taxonomy'     => 'ons_team',
                            ) );
                    foreach ( $terms as $term ) {
                        $term_link = get_term_link( $term, 'ons_team' );
                        if( is_wp_error( $term_link ) )
                        continue;


                    } 
                ?>

This only shows the team members on the panel in which I checked the boxes. But I cannot add the names of the team members to this code.

So I got it to work on the single page using this code:

<?php   
// List of image links

$terms = apply_filters( 'taxonomy-images-get-terms', '', array( 'taxonomy' => 'ons_team' ) );

foreach( (array) $terms as $term) {
echo '<div class="col s6 m6 l3 teamimage"><a href="' . esc_attr( get_term_link( $term ) ) . '" title="' . sprintf( __( "%s" ), $term->name ) . '" ' . '>' . wp_get_attachment_image( $term->image_id, 'destacado-proyectos-home' ) . '</a>';

echo '<h2 class="truncate">' . sprintf( __( "%s" ), $term->name ) . '</h2>';
     echo '</div>';
     }
   ?>

But if I use that code on the panel I WANT the information to show up. It shows it across all the panels, and not on the only one I want to show it on.

I tried using a combination of both but then every panel still shows the images and names.

 <?php $terms = get_the_terms( $post->ID , 'ons_team' ); 
                    $terms = apply_filters( 'taxonomy-images-get-terms', '', array( 'taxonomy' => 'ons_team' ) );

                          foreach( (array) $terms as $term) {
                            echo '<div class="col s6 m6 l3 teamimage"><a href="' . esc_attr( get_term_link( $term ) ) . '" title="' . sprintf( __( "%s" ), $term->name ) . '" ' . '>' . wp_get_attachment_image( $term->image_id, 'destacado-proyectos-home' ) . '</a>';
                            echo '<h2 class="truncate">' . sprintf( __( "%s" ), $term->name ) . '</h2>';
                            echo '</div>';
                          }
                    foreach ( $terms as $term ) {
                        $term_link = get_term_link( $term, 'ons_team' );
                        if( is_wp_error( $term_link ) )
                        continue;


                    } 
                ?>

Placing the code between the foreach or any other place kind of breaks the code. Is it possible that this doesn't work because I'm using $term / $terms alot?

Using it like this:

<?php $terms = get_the_terms( $post->ID , 'ons_team' ); 

                    foreach ( $terms as $term ) {
                        $term_link = get_term_link( $term, 'ons_team' );
                        $terms = apply_filters( 'taxonomy-images-get-terms', '', array( 'taxonomy' => 'ons_team' ) );

                          foreach( (array) $terms as $term) {
                            echo '<div class="col s6 m6 l3 teamimage"><a href="' . esc_attr( get_term_link( $term ) ) . '" title="' . sprintf( __( "%s" ), $term->name ) . '" ' . '>' . wp_get_attachment_image( $term->image_id, 'destacado-proyectos-home' ) . '</a>';
                            echo '<h2 class="truncate">' . sprintf( __( "%s" ), $term->name ) . '</h2>';
                            echo '</div>';
                          }
                        if( is_wp_error( $term_link ) )
                        continue;


                    } 
                ?>

Kind of works, but then it show the team member like 10 times in a row...

Any help is much appreciated!

Solved it!

Here is the correct code if people need it:

 <div class="teamleden over-ons-ul">
                <div class="center-align">
                  <div class="row">
                      <?php   
                       $terms = get_the_terms( $post->ID , 'ons_team' );

                         if ( $terms != null ){
                            $terms = apply_filters( 'taxonomy-images-get-terms', '', array( 'taxonomy' => 'ons_team' ) );

                                  foreach( (array) $terms as $term) {
                                    echo '<div class="col s6 m6 l3 teamimage"><a href="' . esc_attr( get_term_link( $term ) ) . '" title="' . sprintf( __( "%s" ), $term->name ) . '" ' . '>' . wp_get_attachment_image( $term->image_id, 'destacado-proyectos-home' ) . '</a>';
                                    echo '<h2 class="truncate">' . sprintf( __( "%s" ), $term->name ) . '</h2>';
                                    echo '</div>';
                                  }
                         foreach( $terms as $term ) {

                         unset($term);
                        } } ?>
                    </div>
                </div>
            </div>    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM