wordpress taxonomy_exists()和post_type_exists()-返回false

时间:2018-10-16 11:10:41

标签: wordpress custom-post-type

我对自定义帖子类型和自定义分类有疑问。调用taxonomy_exists()和发布类型后,都返回false。但是如果使用CPT,我可以插入新帖子,如果使用自定义分类法,则Wordpress无法处理wp_insert_term()。我尝试了很多解决方案。你知道问题出在哪里吗?

代码:

add_action( 'init', 'car_post_types', apply_filters('car_post_types', 5));

function car_post_types() {
/* Ustawienie argumentów dla typu wpisu samochod. */
          $car_args = array(
              'public' => true,
              'query_var' => true,
              'exclude_from_search' => false,
              'publicly_queryable' => true,
              'show_ui' => true,
              'rewrite' => array(
              'slug' => 'samochod',
              'with_front' => true,
              ),
              'capability_type' => 'post',
              'with_front' => true,
              'supports' => array(
                  'title',
                  'thumbnail',
                  'excerpt',
                  'editor',
                  'author',
                  'comments',
                  'trackbacks',
                  'custom-fields',
                  'page-attributes',
                  'revisions'
              ),
              'labels' => array(
                 'name' => 'Samochody',
                 'singular_name' => 'Samochod',
                 'add_new' => 'Dodaj nowy samochod',
                 'add_new_item' => 'Dodaj nowy samochod',
                 'edit_item' => 'Edytuj samochod',
                 'new_item' => 'Nowy samochod',
                 'view_item' => 'Wyświetl samochod',
                 'search_items' => 'Szukaj w samochodach',
                 'not_found' => 'Nie znaleziono samochodow',
                 'not_found_in_trash' => 'Nie znaleziono samochodu w koszu'
              ),
              'has_archives' => true
         );
  /* Rejestracja typu wpisu samochod. */
register_post_type( 'car', $car_args, 10);
}
add_action( 'init', 'car_taxonomies', 10 );
function car_taxonomies() {
  $cars_args = array(
         'hierarchical' => true,
         'query_var' => 'models',
         'public'=> true,
         '_builtin' => true,
         'rewrite' => array(
         'slug' => 'model',
         'with_front' => true ,
         'capabilities' => array(
                'assign_terms' => 'edit_terms',
                'edit_terms' => 'publish_terms'
            )),
        'labels' => array(
        'name' => 'Marki',
        'singular_name' => 'Marka',
        'edit_item' => 'Edytuj markę', 'update_item' => 'Uaktualnij markę', 'add_new_item' => 'Dodaj nowa markę', 'new_item_name' => 'Nowa nazwa marki', 'all_items' => 'Wszystkie marki', 'search_items' => 'Wyszukaj marki', 'parent_item' => 'Marka nadrzędny', 'parent_item_colon' => 'Marka nadrzędny:',
        ), 
    );
  register_taxonomy('models','car', $cars_args, 0 );
}

2 个答案:

答案 0 :(得分:1)

<?php
    function engine_register_post_type( $name, $slug, $labels, $supports,$exclude_from_search = false) {
        if(!$labels) {
            $labels = array(
                'name' => ucfirst($name),
                'singular_name' => ucfirst($name),
                'add_new' => __('Dodaj nowe', 'engine'),
                'add_new_item' => __('Dodaj nowe', 'engine'),
                'edit_item' => __('Edytuj', 'engine'),
                'new_item' => __('Nowe', 'engine'),
                'view_item' => __('Zobacz', 'engine'),
                'search_items' => __('Szukaj', 'engine'),
                'not_found' =>  __('Brak','engine'),
                'not_found_in_trash' => __('Brak','engine'), 
                'parent_item_colon' => ''
              );
          }

          $args = array(
            'labels' => $labels,
            'public' => true,
            'exclude_from_search' => $exclude_from_search,
            'publicly_queryable' => true,
            'show_ui' => true, 
            'query_var' => true,
            'capability_type' => 'post',
            'hierarchical' => false,
            'menu_position' => null,
            'rewrite' => array('slug' => $slug),
            'supports' => $supports, 
            'taxonomies' => array('groups', 'post_tag') 
          ); 
          register_post_type( strtolower($slug), $args );
    }



    function engine_register_taxonomy($name, $slug, $posttype, $hierarchical = true, $is_tag = false) {

        if (!is_array($posttype)) $posttype = array($posttype);

        register_taxonomy(
            $slug, 
            $posttype, 
            array(
                "hierarchical" => $hierarchical,
                "label" => $name, 
                "show_tagcloud" => $is_tag,
                "singular_label" => ucfirst($name), 
                "rewrite" => 
                    array(
                        'slug' => strtolower($slug), 
                         'hierarchical' => true,
                         'with_front' => true,
                    )
            )
        ); 
    }

    ?>

我编写了新功能来添加CPT和自定义分类法,现在很好。感谢您的帮助!

答案 1 :(得分:0)

您已将功能()放在rewrite()内,尝试取出:

function car_taxonomies() {
      $cars_args = array(
             'hierarchical' => true,
             'query_var' => 'models',
             'public'=> true,
             '_builtin' => true,
             'rewrite' => array(
                    'slug' => 'model',
                    'with_front' => true ,
             ),
             'capabilities' => array(
                    'assign_terms' => 'edit_terms',
                    'edit_terms' => 'publish_terms'
                ),
            'labels' => array(
            'name' => 'Marki',
            'singular_name' => 'Marka',
            'edit_item' => 'Edytuj markę', 'update_item' => 'Uaktualnij markę', 'add_new_item' => 'Dodaj nowa markę', 'new_item_name' => 'Nowa nazwa marki', 'all_items' => 'Wszystkie marki', 'search_items' => 'Wyszukaj marki', 'parent_item' => 'Marka nadrzędny', 'parent_item_colon' => 'Marka nadrzędny:',
            ), 
        );
      register_taxonomy('models','car', $cars_args, 0 );
    }