Last active
October 20, 2016 13:05
-
-
Save aligator28/0d13475f34f7bb7a2ff7c4c9d3f6000b to your computer and use it in GitHub Desktop.
Добавить категории к кастом пост тайпу (categories for custom post type) Wordpress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// add these to functions.php or in Sage to sage/lib/some-file-name.php (do not forget to add in functions.php to load this file) | |
<?php | |
// | |
add_filter('pre_get_posts', 'query_post_type'); | |
function query_post_type($query) { | |
if( is_category() ) { | |
$post_type = get_query_var('post_type'); | |
if($post_type) | |
$post_type = $post_type; | |
else | |
//перечисляем типы постов к которым хотим добавить категории (в моем случае это тип 'menu') | |
$post_type = array('nav_menu_item', 'post', 'menu'); // don't forget nav_menu_item to allow menus to work! | |
$query->set('post_type',$post_type); | |
return $query; | |
} | |
} | |
add_action( 'init', 'sage_create_post_type' ); | |
function sage_create_post_type() { | |
// тут просто регистрируем новый тип поста | |
register_post_type( 'menu', | |
array( | |
'labels' => array( | |
'name' => __( 'Menus' ), | |
'singular_name' => __( 'Menu' ), | |
'taxonomies' => array( 'category' ), | |
), | |
'public' => true, | |
'has_archive' => true, | |
'menu_icon' => 'dashicons-clipboard' | |
) | |
); | |
// эта строка добавляет в админке в режиме редактирования вывод категорий к которым можно привязать тип поста | |
register_taxonomy_for_object_type( 'category', 'menu' ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment