【问题标题】:List Categories of a post and style each by slug按 slug 列出帖子的类别和样式
【发布时间】:2017-10-30 00:37:26
【问题描述】:

我正在尝试在 Wordpress 网站中显示分配给任何帖子的类别,并使用类别 slug 作为类名称为每个类别设置独特的背景颜色。

我尝试了下面的代码,它显示了任何帖子的类别列表,但复制了列表并将 a slug 类添加到每个列表而不是每个类别。我确定重复是由于两个 foreach 循环造成的,但除非我有另一个,否则我无法无误地工作。

<?php $terms = get_the_terms( $post->ID , 'category');
        if($terms) {
            foreach( $terms as $term ) {
                $categories = get_the_category();
                $separator = ' ';
                $output = '';
                    foreach( $categories as $category ) {
                        $cat_obj = get_term($term->term_id, 'category');
                        $cat_slug = $cat_obj->slug;
                        $output .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" class="post-category-' . esc_attr($cat_slug) . '" alt="' . esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ) . '">' . esc_html( $category->name ) . '</a>' . $separator;
                    }

            echo trim( $output, $separator );
            }
        }
    ?>

我最终得到的是一个列表,其中第一个类别 slug 作为一个类,然后是一个第二个列表,其中第二个类别 slug 作为一个类:

<a href="http:site.ca/category/archive-posts/" class="post-category-archive-posts" alt="View all posts in Archive Posts">Archive Posts</a>
<a href="http://site.ca/category/making-decisions/" class="post-category-archive-posts" alt="View all posts in Decision Making">Decision Making</a>
<a href="http://site.ca/category/archive-posts/" class="post-category-making-decisions" alt="View all posts in Archive Posts">Archive Posts</a>
<a href="http:site.ca/category/making-decisions/" class="post-category-making-decisions" alt="View all posts in Decision Making">Decision Making</a>

任何有关如何解决重复的帮助将不胜感激。

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    如果您要使用不同的函数遍历可用类别,则无需遍历类别术语。 get_the_category()的返回对象也存在Slug对象属性

    <?php $categories = get_the_category();
            if(!empty( $categories)) {
                    $separator = ' ';
                    $output = '';
                        foreach( $categories as $category ) {
                            $cat_obj = get_term($term->term_id, 'category');
                            // $cat_slug = $cat_obj->slug;
                            $output .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" class="post-category-' . esc_attr($category->slug) . '" alt="' . esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ) . '">' . esc_html( $category->name ) . '</a>' . $separator;
                        }
    
                echo trim( $output, $separator );
            }
        ?>
    

    【讨论】:

      【解决方案2】:

      get_the_category() 已经提供了您需要的所有信息。不需要所有额外的循环或数据库查询:

      <?php
        $the_cats = get_the_category();
        if( $the_cats ) ){
          $links = array();
          foreach( $the_cats as $the_cat ) {
            $links[] = '<a href="' . esc_url( get_category_link( $the_cat->term_id ) ) . '" class="post-category-' . esc_attr( $the_cat->slug ) . '" alt="' . esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $the_cat->name ) ) . '">' . esc_html( $the_cat->name ) . '</a>';
          }
          echo implode( ' ', $links );
        }
      ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-11-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多