共計(jì) 1786 個(gè)字符,預(yù)計(jì)需要花費(fèi) 5 分鐘才能閱讀完成。
今天丸趣 TV 小編給大家分享一下 wordpress 怎么對(duì)分類法文章進(jìn)行篩選的相關(guān)知識(shí)點(diǎn),內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識(shí),所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。
如何注冊(cè)自己的自定義分類法呢?
首先打開 wordpress 核心函數(shù)文件 functions.php,在適當(dāng)位置插入以下代碼
function my_custom_post_courses() {
$labels = array( name = _x( courses , post type 名稱 ),
singular_name = _x( my_courses , post type 單個(gè) item 時(shí)的名稱),
add_new = _x( 增加課程 , 添加新內(nèi)容的鏈接名稱 ),
add_new_item = __( 增加一個(gè)課程 ),
edit_item = __( 編輯課程 ),
new_item = __( 新課程 ),
all_items = __( 所有課程 ),
view_item = __( 查看課程 ),
search_items = __( 搜索課程 ),
not_found = __( 沒有找到有關(guān)課程 ),
not_found_in_trash = __( 回收站里面沒有相關(guān)課程 ),
parent_item_colon = ,
menu_name = 課程
);
$args = array(
labels = $labels,
description = 我們網(wǎng)站的課程信息 ,
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 );
這樣我們就注冊(cè)好了一個(gè)名稱為‘課程’的自定義分類法,接下來就是如何設(shè)置篩選文章。
如果你的文章或產(chǎn)品很多,我們就有必要在 WordPress 后臺(tái)文章列表頁(yè)添加篩選功能,如下圖:
要實(shí)現(xiàn)篩選的功能,需要兩個(gè)步驟
第一步,同樣是在 functions.php 文件中添加后臺(tái)的功能,讓列表頁(yè)顯示這個(gè)分類選擇
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,
));
}
}
第二步,實(shí)現(xiàn)篩選的功能
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;
}
}
以上就是“wordpress 怎么對(duì)分類法文章進(jìn)行篩選”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,丸趣 TV 小編每天都會(huì)為大家更新不同的知識(shí),如果還想學(xué)習(xí)更多的知識(shí),請(qǐng)關(guān)注丸趣 TV 行業(yè)資訊頻道。