WordPress » Thủ thuật wordpress » Thêm lọc bài viết cho custom post type trong admin wordpress

Thêm lọc bài viết cho custom post type trong admin wordpress

Bởi administrator | 774

Nếu các bạn muốn làm như hình dưới đây thì

Các bạn copy ném vào function.php thay đổi postype và taxonomy rồi F5 lại website .

function filter_cars_by_taxonomies( $post_type, $which ) {

  // Apply this only on a specific post type
  if ( 'product' !== $post_type )
    return;

  // A list of taxonomy slugs to filter by
  $taxonomies = array( 'product_category');

  foreach ( $taxonomies as $taxonomy_slug ) {

    // Retrieve taxonomy data
    $taxonomy_obj = get_taxonomy( $taxonomy_slug );
    $taxonomy_name = $taxonomy_obj->labels->name;

    // Retrieve taxonomy terms
    $terms = get_terms( $taxonomy_slug );

    // Display filter HTML
    echo "<select name='{$taxonomy_slug}' id='{$taxonomy_slug}' class='postform'>";
    echo '<option value="">' . sprintf( esc_html__( 'Show All %s', 'text_domain' ), $taxonomy_name ) . '</option>';
    foreach ( $terms as $term ) {
      printf(
        '<option value="%1$s" %2$s>%3$s (%4$s)</option>',
        $term->slug,
        ( ( isset( $_GET[$taxonomy_slug] ) && ( $_GET[$taxonomy_slug] == $term->slug ) ) ? ' selected="selected"' : '' ),
        $term->name,
        $term->count
      );
    }
    echo '</select>';
  }

}
add_action( 'restrict_manage_posts', 'filter_cars_by_taxonomies' , 10, 2);

Chúc các bạn thành công !

Hãy like nếu bạn thấy hữu ích

Leave a Reply

Your email address will not be published. Required fields are marked *

Bài viết liên quan