Home>Article>CMS Tutorial> How to filter taxonomy articles in WordPress?
The followingWordPresstutorial column will introduce to you how WordPress filters background articles. I hope it will be helpful to friends in need!
In addition to its own article classification, wordpress also supports custom classifications, that is, you can register custom article classifications according to your own needs.
How to register your own custom taxonomy?
First open the WordPress core function file functions.php and insert the following code in the appropriate location
function my_custom_post_courses() { $labels = array( 'name' => _x( 'courses', 'post type 名称' ), 'singular_name' => _x( 'my_courses', 'post type 单个 item 时的名称'), 'add_new' => _x( '增加课程', '添加新内容的链接名称' ), 'add_new_item' => __( '增加一个课程' ), 'edit_item' => __( '编辑课程' ), 'new_item' => __( '新课程' ), 'all_items' => __( '所有课程' ), 'view_item' => __( '查看课程' ), 'search_items' => __( '搜索课程' ), 'not_found' => __( '没有找到有关课程' ), 'not_found_in_trash' => __( '回收站里面没有相关课程' ), 'parent_item_colon' => '', 'menu_name' => '课程' ); $args = array( 'labels' => $labels, 'description' => '我们网站的课程信息', 'public' => true, 'menu_position' => 5, 'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ), 'has_archive' => true ); register_post_type( 'my_courses', $args ); } add_action( 'init', 'my_custom_post_courses' );
In this way, we have registered a custom taxonomy named 'Course'. The next step is how to set up filtered articles.
If you have a lot of articles or products, it is necessary for us to add a filtering function to the WordPress backend article list page, as shown below:
To achieve filtering Function requires two steps
The first step is to add the background function in the functions.php file and let the list page display this category selection
add_action('restrict_manage_posts', 'mycat_add_select_int'); function mycat_add_select_int(){ global $typenow; global $wp_query; if ($typenow == 'course') { return wp_dropdown_categories(array( 'show_option_all' => '课程分类', 'taxonomy' => 'course_list', 'name' => 'course_list', 'orderby' => 'name', 'selected' => @$wp_query->query['course_list'], 'hierarchical' => true, 'depth' => 5, 'show_count' => true, 'hide_empty' => false, )); } }
The second step is to implement filtering Function
add_filter('parse_query', 'mycats_add_query_init'); function mycats_add_query_init($query){ $qv = &$query->query_vars; if (($qv['course_list']) && is_numeric($qv['course_list'])) { $term = get_term_by('id', $qv['course_list'],'course_list'); $qv['course_list'] = $term->slug; } }
Only the above two steps are needed to easily implement the background article filtering function, which is very practical for blog webmasters or webmasters with many background articles on their websites.
The above is the detailed content of How to filter taxonomy articles in WordPress?. For more information, please follow other related articles on the PHP Chinese website!