简体   繁体   English

从自定义分类术语中获取图像

[英]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. 因此,我对ons_team进行了自定义分类,因为我不想在每个面板上都显示团队成员。 So I checked the boxes on the ctp where I want the team members to be shown. 因此,我选中了希望显示团队成员的ctp框。

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. 将代码放在foreach或任何其他地方类型之间会破坏代码。 Is it possible that this doesn't work because I'm using $term / $terms alot? 是否有可能因为我大量使用$ term / $ terms而不起作用?

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... 有点工作,但随后连续10次向团队成员展示...

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>    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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