Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aligator28/0d13475f34f7bb7a2ff7c4c9d3f6000b to your computer and use it in GitHub Desktop.
Save aligator28/0d13475f34f7bb7a2ff7c4c9d3f6000b to your computer and use it in GitHub Desktop.
Добавить категории к кастом пост тайпу (categories for custom post type) Wordpress
// 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