0

I've installed a theme and I am trying to follow its logic and hierarchy in order to register and display a new taxonomy .so I have added a new post type called product (which displays correctly in the admin panel) and now I want to add new taxonomy called product category to it .

However, this new taxonomy doesn't show in the admin I dont know why? Here's the code:

       class SH_Taxonomies
      {
    function __construct()
       {
    // Hook into the 'init' action
add_action( 'init', array($this, 'taxonomies'), 0 ); }

    // Register Custom Taxonomy
        function taxonomies()  {

       register taxonomy
           $labels = array(
            'name'                       => _x( 'Category', 'Product Category', SH_NAME ),
        'singular_name'              => _x( 'Category', 'Category', SH_NAME ),
        'menu_name'                  => __( 'Category', SH_NAME ),
        'all_items'                  => __( 'All Categories', SH_NAME ),
        'parent_item'                => __( 'Parent Category', SH_NAME ),
        'parent_item_colon'          => __( 'Parent Category:', SH_NAME ),
        'new_item_name'              => __( 'New Category Name', SH_NAME ),
        'add_new_item'               => __( 'Add New Category', SH_NAME ),
        'edit_item'                  => __( 'Edit Category', SH_NAME ),
        'update_item'                => __( 'Update Category', SH_NAME ),
        'separate_items_with_commas' => __( 'Separate Categories with commas', SH_NAME ),
        'search_items'               => __( 'Search Categories', SH_NAME ),
        'add_or_remove_items'        => __( 'Add or remove Categories', SH_NAME ),
        'choose_from_most_used'      => __( 'Choose from the most used Categories', SH_NAME ),
    );

    $rewrite = array(
        'slug'                       => 'product_category',
        'with_front'                 => true,
        'hierarchical'               => true,
    );

    $args = array(
        'labels'                     => $labels,
        'hierarchical'               => true,
        'public'                     => true,
        'show_ui'                    => true,
        'show_admin_column'          => true,
        'show_in_nav_menus'          => true,
        'show_tagcloud'              => true,
        'rewrite'                    => $rewrite,
    );

    register_taxonomy( 'product_category', 'sh_product', $args );
    }
    }
2
  • 1
    Do you have a specific question, problem or error message to resolve?
    – jdm2112
    Commented May 7, 2014 at 20:23
  • What does "doesn't work" exactly mean? And please file an edit and fix your code MarkUp. Thanks.
    – kaiser
    Commented May 7, 2014 at 23:04

1 Answer 1

1

You have a couple of bugs in your code and some other errors. Here are they

Firstly

Parse error: syntax error, unexpected T_STRING in ....\functions\toets.php on line 12

This is this piece of code that should not be there at all.

 register taxonomy

You can simply just remove that. It has got no function there at all

Secondly, you actually need to register your class. You can do this by just simply doing this

$taxregister = new SH_Taxonomies();

Unrelated, but a bug (or actually a couple of bugs)

Notice: Use of undefined constant SH_NAME - assumed 'SH_NAME' in ...\functions\toets.php on line 13

, your text domain name should be in ', so

_x( 'Category', 'Product Category', SH_NAME ),

should be

_x( 'Category', 'Product Category', 'SH_NAME' ),

Just a point of note, your text domain should be the same as the one registered in your style.css if this is in a theme

So your complete code that works should be

<?php
  class SH_Taxonomies
      {
    function __construct()
       {
    // Hook into the 'init' action
add_action( 'init', array($this, 'taxonomies'), 0 ); }

    // Register Custom Taxonomy
        function taxonomies()  {

           $labels = array(
            'name'                       => _x( 'Category', 'Product Category', 'SH_NAME' ),
        'singular_name'              => _x( 'Category', 'Category', 'SH_NAME' ),
        'menu_name'                  => __( 'Category', 'SH_NAME' ),
        'all_items'                  => __( 'All Categories', 'SH_NAME' ),
        'parent_item'                => __( 'Parent Category', 'SH_NAME' ),
        'parent_item_colon'          => __( 'Parent Category:', 'SH_NAME' ),
        'new_item_name'              => __( 'New Category Name', 'SH_NAME' ),
        'add_new_item'               => __( 'Add New Category', 'SH_NAME' ),
        'edit_item'                  => __( 'Edit Category', 'SH_NAME' ),
        'update_item'                => __( 'Update Category', 'SH_NAME' ),
        'separate_items_with_commas' => __( 'Separate Categories with commas', 'SH_NAME' ),
        'search_items'               => __( 'Search Categories', 'SH_NAME' ),
        'add_or_remove_items'        => __( 'Add or remove Categories', 'SH_NAME' ),
        'choose_from_most_used'      => __( 'Choose from the most used Categories', 'SH_NAME' ),
    );

    $rewrite = array(
        'slug'                       => 'product_category',
        'with_front'                 => true,
        'hierarchical'               => true,
    );

    $args = array(
        'labels'                     => $labels,
        'hierarchical'               => true,
        'public'                     => true,
        'show_ui'                    => true,
        'show_admin_column'          => true,
        'show_in_nav_menus'          => true,
        'show_tagcloud'              => true,
        'rewrite'                    => $rewrite,
    );

    register_taxonomy( 'product_category', 'sh_product', $args );
    }
    }

$taxregister = new SH_Taxonomies();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.