TODO: To change the header's content go to Dr.Explain menu Options : Project Settings : HTML (CHM) Export : Setup HTML Template and Layout
×
Menu
Index

Tùy biến hiển thị categories nâng cao

 
Giống cách wordpress hiển thị menu, với category/taxonomy chúng ta có Walker_Category cho phép chúng ta thay đổi cách taxonomy hiển thị.
Bài tập: Hiển thị tất cả những categories, categories nào thuộc về post có chỉ định $id xác định.
class Post_Category_Walker extends Walker_Category {
 
    private $term_ids = array();
 
    function __construct( $post_id, $taxonomy )  {
        // fetch the list of term ids for the given post
        $this->term_ids = wp_get_post_terms( $post_id, $taxonomy, 'fields=ids' );
    }
 
    function display_element( $element, &$children_elements, $max_depth, $depth=0, $args, &$output ) {
        $display = false;
 
        $id = $element->term_id;
 
        if ( in_array( $id, $this->term_ids ) ) {
            // the current term is in the list
            $display = true;
        }
        elseif ( isset( $children_elements[ $id ] ) ) {
            // the current term has children
            foreach ( $children_elements[ $id ] as $child ) {
                if ( in_array( $child->term_id, $this->term_ids ) ) {
                    // one of the term's children is in the list
                    $display = true;
                    // can stop searching now
                    break;
                }
            }
        }
 
        if ( $display )
            parent::display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output );
    }
}
 
Ở đây chúng ta thấy có hàm display_element(), hàm này được gọi cho mỗi category và quyết định cho từng category được hiển thị hoặc không.
Walker Class trên sẽ sử dụng vào tham số 'walker' nói với wordpress cách lấy dữ liệu categories.
function walk_post_categories( $post_id, $args = array() ) {
    $args = wp_parse_args( $args, array(
        'taxonomy' => 'category'
    ) );
    $args['title_li'] = ' ';
    $args['walker'] = new Post_Category_Walker( $post_id, $args['taxonomy'] );
    $output = wp_list_categories( $args );
    if ( $output )
        return $output;
}
 
 
Gọi hàm walk_post_categories vào template ở vị trí bạn muốn hiển thị.
<?php walk_post_categories(123);?>
<?php walk_post_categories(123, 'show_count=1&title_li='); ?>
 
Tham khảo tạo lớp Walker_Category đầy đủ:
class hw_Walker_categories extends Walker_Category {
   
    function __construct(){
       
    }
    public function start_lvl( &$output, $depth = 0, $args = array() ) {
        if ( 'list' != $args['style'] )
            return;
    
        $indent = str_repeat("\t", $depth);
        $output .= "$indent<ul class='children'>\n";
    }
 
    public function end_lvl( &$output, $depth = 0, $args = array() ) {
        if ( 'list' != $args['style'] )
            return;
    
        $indent = str_repeat("\t", $depth);
        $output .= "$indent</ul>\n";
    }
 
    public function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
        /** This filter is documented in wp-includes/category-template.php */
        $cat_name = apply_filters(
            'list_cats',
            esc_attr( $category->name ),
            $category
        );
    
        $link = '<a href="' . esc_url( get_term_link( $category ) ) . '" ';
        if ( $args['use_desc_for_title'] && ! empty( $category->description ) ) {
            $link .= 'title="' . esc_attr( strip_tags( apply_filters( 'category_description', $category->description, $category ) ) ) . '"';
        }
    
        $link .= '>';
        $link .= $cat_name . '</a>';
    
        if ( ! empty( $args['feed_image'] ) || ! empty( $args['feed'] ) ) {
            $link .= ' ';
    
            if ( empty( $args['feed_image'] ) ) {
                $link .= '(';
            }
    
            $link .= '<a href="' . esc_url( get_term_feed_link( $category->term_id, $category->taxonomy, $args['feed_type'] ) ) . '"';
    
            if ( empty( $args['feed'] ) ) {
                $alt = ' alt="' . sprintf(__( 'Feed for all posts filed under %s' ), $cat_name ) . '"';
            } else {
                $alt = ' alt="' . $args['feed'] . '"';
                $name = $args['feed'];
                $link .= empty( $args['title'] ) ? '' : $args['title'];
            }
    
            $link .= '>';
    
            if ( empty( $args['feed_image'] ) ) {
                $link .= $name;
            } else {
                $link .= "<img src='" . $args['feed_image'] . "'$alt" . ' />';
            }
            $link .= '</a>';
    
            if ( empty( $args['feed_image'] ) ) {
                $link .= ')';
            }
        }
    
        if ( ! empty( $args['show_count'] ) ) {
            $link .= ' (' . number_format_i18n( $category->count ) . ')';
        }
        if ( 'list' == $args['style'] ) {
            $output .= "\t<li";
            $class = 'cat-item cat-item-' . $category->term_id;
            if ( ! empty( $args['current_category'] ) ) {
                $_current_category = get_term( $args['current_category'], $category->taxonomy );
                if ( $category->term_id == $args['current_category'] ) {
                    $class .=  ' current-cat';
                } elseif ( $category->term_id == $_current_category->parent ) {
                    $class .=  ' current-cat-parent';
                }
            }
            $output .=  ' class="' . $class . '"';
            $output .= ">$link\n";
        } else {
            $output .= "\t$link<br />\n";
        }
    }
   
    public function end_el( &$output, $page, $depth = 0, $args = array() ) {
        if ( 'list' != $args['style'] )
            return;
    
        $output .= "</li>\n";
    }
}
Made with help of Dr.Explain

Unregistered version