Creating a Custom Post-Type in Wordpress with Cust

2019-01-28 20:34发布

问题:

I need to build a custom post type for the Toys I'm selling. The custom post type I want to create is "Toys". I want them to have a categories/tags so I can sort them later, Tags I want to create for now is "Bath Toys", "Magnets", "Yoyos", and "Glow in the Dark".

I think if I can observe the code, I can try to analyze it and just replicate it later.

Here's the tutorial that I've been trying to follow. But it still confuses me how to add taxonomies or tags.

I am adding this functions to the functions.php of my child theme and I am using WordPress 3.3.1

回答1:

You want to define your taxonomy and custom post type in your functions.php using the register_taxonomy() and register_post_type() functions.

Here is an example what it could look like:

/****************************************
 * Add custom taxonomy for Toys *
 ****************************************/

add_action('init', 'toys_categories_register');

function toys_categories_register() {
$labels = array(
    'name'                          => 'Toys Categories',
    'singular_name'                 => 'Toys Category',
    'search_items'                  => 'Search Toys Categories',
    'popular_items'                 => 'Popular Toys Categories',
    'all_items'                     => 'All Toys Categories',
    'parent_item'                   => 'Parent Toy Category',
    'edit_item'                     => 'Edit Toy Category',
    'update_item'                   => 'Update Toy Category',
    'add_new_item'                  => 'Add New Toy Category',
    'new_item_name'                 => 'New Toy Category',
    'separate_items_with_commas'    => 'Separate toys categories with commas',
    'add_or_remove_items'           => 'Add or remove toys categories',
    'choose_from_most_used'         => 'Choose from most used toys categories'
    );

$args = array(
    'label'                         => 'Toys Categories',
    'labels'                        => $labels,
    'public'                        => true,
    'hierarchical'                  => true,
    'show_ui'                       => true,
    'show_in_nav_menus'             => true,
    'args'                          => array( 'orderby' => 'term_order' ),
    'rewrite'                       => array( 'slug' => 'toys', 'with_front' => true, 'hierarchical' => true ),
    'query_var'                     => true
);

register_taxonomy( 'toys_categories', 'toys', $args );
}

/*****************************************
 * Add custom post type for Toys *
 *****************************************/

add_action('init', 'toys_register');

function toys_register() {

    $labels = array(
        'name' => 'Toys',
        'singular_name' => 'Toy',
        'add_new' => 'Add New',
        'add_new_item' => 'Add New Toy',
        'edit_item' => 'Edit Toy',
        'new_item' => 'New Toy',
        'view_item' => 'View Toy',
        'search_items' => 'Search Toys',
        'not_found' =>  'Nothing found',
        'not_found_in_trash' => 'Nothing found in Trash',
        'parent_item_colon' => ''
    );

    $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'query_var' => true,
        'has_archive' => true,
        'rewrite' => array( 'slug' => 'toys', 'with_front' => true ),
        'capability_type' => 'post',
        'menu_position' => 6,
        'supports' => array('title', 'excerpt', 'editor','thumbnail') //here you can specify what type of inputs will be accessible in the admin area
      );

    register_post_type( 'toys' , $args );
}

Then you need to go to the admin back-end and you should see Toys right below Post, create categories you desire in 'Toys Categories'.



回答2:

I know i am late but this might help those who are struggling to find an easy way to add WP Custom Post Types.

There is an awesome library to work with WordPress Post types and Taxonomies.

Take these steps this will make your life simpler.

  1. in your themes directory run this command.

    $ composer require azi/raskoh

  2. include composer autoloader to your themes functions.php

    require_once "vendor/autoloader.php";

  3. after require add this code to register post type

`

$toy = new Raskoh\PostType("Toy");

$toy->register();

the Raskoh (library) will take care of the rest. here is the library on

Github