Advertisement
  1. Code
  2. WordPress

Створення структури бази знань на Wordpress

Scroll to top
Read Time: 5 min

Ukrainian (українська мова) translation by Bohdan Alexandrov (you can also view the original English article)

На даному етапі Ви вже повинні мати план структури Вашої бази знань, типи постів та класифікації, що необхідні для збереження інформації.

Наступний крок - створення структури для Ваших даних.

В цьому уроці я розповім Вам про:

  • реєстрацію класифікації для Вашої інформації
  • видалення класифікацій, що Вам не потрібні

Що Вам потрібно?

Для проходження даного уроку Вам необхідно:

  • пакет інсталяції WordPress
  • текстовий редактор
  • початкову тему

1. Створення початкової теми

В якості початкової теми я буду використовувати тему, що розробив в попередньому уроці створюючи тему для WordPress. Тема знаходиться у вкладенні коду до цього уроку. Якщо Ви бажаєте можете обрати власну тему або завантажити її з бази офіційних тем.

Перед тим як почати, якщо Ви використовуєте тему, що Ви завантажили, Ви повинні перейменувати її папку та відредагувати файл стилів, щоб показати нове застосування теми.

Отже, мій файл стилів зараз має на початку наступне:

1
/*

2
Theme Name: Creating a Knowledge Base in WordPress - starting theme

3
Theme URI: https://rachelmccollin.co.uk

4
Author: Rachel McCollin

5
Author URI: http://rachelmccollin.co.uk

6
Description: The theme to accompany the wptutsplus series on creating a knowledge base using WordPress. This is the starting theme.

7
Version: 1.0

8
License: GNU General Public License v2 or later

9
License URI: http://www.gnu.org/licenses/gpl-2.0.html

10
*/

2. Реєстрація типів постів та класифікацій

Якщо Ваша база знань зроблена задопомогою стандартних типів записів, тоді Вам потрібно зареєструвати ці. Ви зробите це додавши функцію register_post_type() до файлу functions.php Вашої теми.

Моя база знань буде використовувати лише записи із стандартними класифікаціями, тому мені необхідно використати функцію register_taxonomy()

Відкрийте Ваш файл functions.php . Якщо Ви використовуєте мою початкову тему, вона вже має деякі функції, які додають в темі підтримку популярних зображень та реєструють деякі віджети. Нижче останньої функції, додайте наступне, щоб зареєструвати класифікацію типу контенту:

1
function tutsplus_taxonomies() {
2
    // Content type taxonomy

3
  $labels = array(
4
		'name'              => __( 'Content Types', 'tutsplus' ),
5
		'singular_name'     => __( 'Content Type', 'tutsplus' ),
6
		'search_items'      => __( 'Search Content Types', 'tutsplus' ),
7
		'all_items'         => __( 'All Content Types', 'tutsplus' ),
8
		'edit_item'         => __( 'Edit Content Type', 'tutsplus' ),
9
		'update_item'       => __( 'Update Content Type', 'tutsplus' ),
10
		'add_new_item'      => __( 'Add New Content Type', 'tutsplus' ),
11
		'new_item_name'     => __( 'New Content Type Name', 'tutsplus' ),
12
		'menu_name'         => __( 'Content Types', 'tutsplus' ),
13
	);
14
15
	$args = array(
16
		'hierarchical'      => true,
17
		'labels'            => $labels,
18
		'show_ui'           => true,
19
		'show_admin_column' => true,
20
		'query_var'         => true,
21
		'rewrite'           => array( 'slug' => 'content-types' ),
22
	);
23
	
24
	register_taxonomy( 'contenttype', array( 'post' ), $args );
25
	
26
}
27
28
add_action( 'init', 'tutsplus_taxonomies' );

Тепер Ви будете мати додаткову класифікацію в панелі адміністратора для записів:

extra taxonomy showing up in the adminextra taxonomy showing up in the adminextra taxonomy showing up in the admin

Наступне, що Вам потрібно це додати додаткові класифікації. Я використовую ще дві теми: для користувачів та для розробників. Всередині функції  tutsplus_taxonomies() , що Ви вже створили, зареєструйте кожну з них використовуючи register_taxonomy() . Вся Ваша функція тепер виглядатиме так:

1
function tutsplus_taxonomies() {
2
    // Content type taxonomy

3
	$labels = array(
4
		'name'              => __( 'Content Types', 'tutsplus' ),
5
		'singular_name'     => __( 'Content Type', 'tutsplus' ),
6
		'search_items'      => __( 'Search Content Types', 'tutsplus' ),
7
		'all_items'         => __( 'All Content Types', 'tutsplus' ),
8
		'edit_item'         => __( 'Edit Content Type', 'tutsplus' ),
9
		'update_item'       => __( 'Update Content Type', 'tutsplus' ),
10
		'add_new_item'      => __( 'Add New Content Type', 'tutsplus' ),
11
		'new_item_name'     => __( 'New Content Type Name', 'tutsplus' ),
12
		'menu_name'         => __( 'Content Types', 'tutsplus' ),
13
	);
14
15
	$args = array(
16
		'hierarchical'      => true,
17
		'labels'            => $labels,
18
		'show_ui'           => true,
19
		'show_admin_column' => true,
20
		'query_var'         => true,
21
		'rewrite'           => array( 'slug' => 'content-types' ),
22
	);
23
	
24
	register_taxonomy( 'contenttype', array( 'post' ), $args );
25
	
26
	// User topic taxonomy

27
	$labels = array(
28
		'name'              => __( 'User Topics', 'tutsplus' ),
29
		'singular_name'     => __( 'User Topic', 'tutsplus' ),
30
		'search_items'      => __( 'Search User Topics', 'tutsplus' ),
31
		'all_items'         => __( 'All User Topics', 'tutsplus' ),
32
		'edit_item'         => __( 'Edit User Topic', 'tutsplus' ),
33
		'update_item'       => __( 'Update User Topic', 'tutsplus' ),
34
		'add_new_item'      => __( 'Add New User Topic', 'tutsplus' ),
35
		'new_item_name'     => __( 'New User Topic Name', 'tutsplus' ),
36
		'menu_name'         => __( 'User Topics', 'tutsplus' ),
37
	);
38
39
	$args = array(
40
		'hierarchical'      => true,
41
		'labels'            => $labels,
42
		'show_ui'           => true,
43
		'show_admin_column' => true,
44
		'query_var'         => true,
45
		'rewrite'           => array( 'slug' => 'user-topics' ),
46
	);
47
48
	register_taxonomy( 'usertopic', array( 'post' ), $args );
49
	
50
	// Developer topic taxonomy

51
	$labels = array(
52
		'name'              => __( 'Developer Topics', 'tutsplus' ),
53
		'singular_name'     => __( 'Developer Topic', 'tutsplus' ),
54
		'search_items'      => __( 'Search Developer Topics', 'tutsplus' ),
55
		'all_items'         => __( 'All Developer Topics', 'tutsplus' ),
56
		'edit_item'         => __( 'Edit Developer Topic', 'tutsplus' ),
57
		'update_item'       => __( 'Update Developer Topic', 'tutsplus' ),
58
		'add_new_item'      => __( 'Add New Developer Topic', 'tutsplus' ),
59
		'new_item_name'     => __( 'New Developer Topic Name', 'tutsplus' ),
60
		'menu_name'         => __( 'Developer Topics', 'tutsplus' ),
61
	);
62
63
	$args = array(
64
		'hierarchical'      => true,
65
		'labels'            => $labels,
66
		'show_ui'           => true,
67
		'show_admin_column' => true,
68
		'query_var'         => true,
69
		'rewrite'           => array( 'slug' => 'developer-topics' ),
70
	);
71
72
	register_taxonomy( 'developertopic', array( 'post' ), $args );
73
74
}
75
76
add_action( 'init', 'tutsplus_taxonomies' );

Тепер Ви маєте три класифікації для використання з постами:

three taxonomies for use with poststhree taxonomies for use with poststhree taxonomies for use with posts

3. Видалення небажаних класифікацій

Моя база знань не буде використовувати категорії, тому я хочу видалити їх з "адмінки" для того щоб люди під час введення інформації випадково не використали їх коли вони будуть використовувати мою класифікацію.

Спосіб задопомогою якого Ви це робите є дещо нелогічним, тому що ця функція не призначена для видалення класифікації. Замість цього, Ви запускаєте функцію register_taxonomy() без параметрів, крім ім'я класифікації.

Для видалення категорій додайте наступне до файлу functions.php :

1
function tutsplus_remove_taxonomies() {
2
3
    register_taxonomy( 'category', '' );
4
	
5
}
6
add_action( 'init', 'tutsplus_remove_taxonomies', 0 );

При стрільбі це через гак init, використовується пріоритет 0 , що гарантує, що ця функція працює після основної функції WordPress налаштовуючи категорії в першу чергу.

Якщо Ви бажаєте видалити теги також, просто додайте наступне до функції tutsplus_rremove_taxonomies() :

1
register_taxonomy( 'post_tag', '' );

Тепер Ви побачите, що лише теги та мої стандартні класифікації доступні в майстерні:

Categories removed in dashboardCategories removed in dashboardCategories removed in dashboard

Підсумки

Зараз Ви маєте структуру, що вже готова для додавання інформації до Вашої бази знань. Наступним кроком буде створення зовнішньої частини, про що я розповім в наступному уроці.  В ньому я покажу як створювати файл шаблону, додавати гачки та створювати динамічні бічні панелі.

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.