【问题标题】:wrap each custom post taxonomy term within an li - using a foreach loop将每个自定义后分类术语包装在一个 li 中 - 使用 foreach 循环
【发布时间】:2013-06-19 13:11:17
【问题描述】:

我正在尝试显示一些自定义帖子分类术语 - 但每个都在它们自己的特定列表项中。

我有一个关于车库的网站。 (在 localhost 上开发,所以还没有链接)。

我有一个名为 Cars 的自定义帖子类型。在这种情况下,我有一个自定义的帖子分类法,在这种情况下是汽车“福特”的品牌。

“福特”内是该车库中所有福特汽车的自定义后分类术语列表。 “GT”、“塞拉”、“猎户座”。

而不是列出术语:“GT”、“Sierra”、“Orion”。我想在 ul 列表中显示汽车的图片。

我已经创建了一个包含所有图像的精灵,并希望循环通过这些设置每个 li 项目的背景位置。

下面的第一批代码显示了一个条款列表,这些条款都很好,但不是我想要的。底部是我尝试添加列表项但只是得到一个空白屏幕的代码...

任何帮助,非常感谢。谢谢

<?php if ( get_post_type() == cars ) { ?>
        <div class="entry-meta-custom">
            <?php
            $terms = get_the_terms( $post->ID, 'ford' );

            if ( $terms && ! is_wp_error( $terms ) ) : 

                 $draught_links = array();

             foreach ( $terms as $term ) {
                 $draught_links[] = $term->name;
             }

              $on_draught = join( ", ", $draught_links );
              ?>

             <?php echo $on_draught; ?>

         <?php endif; ?>
     </div><!-- .entry-meta-custom -->
 <?php } ?>

这是我尝试添加列表项的地方。

<?php if ( get_post_type() == cars ) { ?>
        <div class="entry-meta-custom">     
            <?php
            $terms = get_the_terms( $post->ID, 'ford' );

            if ( $terms && ! is_wp_error( $terms ) ) : 

                $draught_links = array();

            foreach ( $terms as $term ) {

                if ($term->name == 'gt') { 
                    $term->name = '<li class="gt">' . $term->name . '</li>';
                 }
                 if ($term->name == 'sierra') { 
                    $term->name = '<li class="sierra">' . $term->name . '</li>';
                 }
                 if ($term->name == 'orion') { 
                    $term->name = '<li class="orion">' . $term->name . '</li>';
                 }
                $draught_links[] = $term->name;
            }

            $on_draught = join( ", ", $draught_links );
            ?>

            <?php echo '<ul>' . $on_draught . '</ul>; ?>

        <?php endif; ?>
    </div><!-- .entry-meta-custom -->
<?php } ?>

【问题讨论】:

    标签: wordpress custom-post-type taxonomy taxonomy-terms


    【解决方案1】:

    我解决了。下面是任何希望用链接到术语存档的单个图像替换自定义后分类列表术语的人的代码。

    来源http://codex.wordpress.org/Function_Reference/get_term_link

    <?php if ( get_post_type() == cars ) { ?>
        <div class="entry-meta-custom">     
            <?php
    
            $terms = get_terms('ford');
    
            echo '<ul>';
    
            foreach ($terms as $term) {
                echo '<li class="'.$term->name.'"><a href="'.get_term_link($term->slug, 'ford').'"></a></li>';
            }
    
            echo '</ul>'; ?>
    
        </div><!-- .entry-meta-custom -->
    <?php } ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-05
      • 1970-01-01
      • 2017-11-09
      • 2012-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多