简体   繁体   中英

Creating and Showing Custom Post Types in Wordpress Functions.php

Reading the book http://shop.oreilly.com/product/0636920027508.do and I am on page 511 where we define a custom post type in PHP. Using the code below, its supposed to display the created post type: 'product' on the pages.

Mine is not.

Is that code accurate or am I doing something wrong?

 function create_product_post_type() {
   $labels = array (
      'name' => 'Products', 'singular_name'  => 'Product'
  );

    $args = array (
    'labels'  => $labels, 
    'public'  => true, 
    'supports'  => array('title', 'editor', 'thumbnail', 'excerpt'),
    'taxonomies' => array( 'category') );
   register_post_type('product', $args );
 }

add_action( 'init', 'create_product_post_type' );



   function add_product_to_archives( $wp_query ) {
       $types_array = array( 'post', 'product');
   if( is_archive() && empty( $query->query_vars['suppress_filters'] ) ) {
       set_query_var( 'post_type', $types_array );


  }
}
  add_action('pre_get_posts', 'add_product_to_archives');

The book is five years old, and I'm now using the most recent version of WordPress. Do you think this is the issue?

It's possible you're missing some mandatory key for 'register_post_type'. You can check full documentation here: https://developer.wordpress.org/reference/functions/register_post_type/

(I'd recommend you to use Wordpress Codex and Developer Code Reference instead of offline books. Info changes from versions and those are the places to keep it updated):

Try this code:

if ( ! function_exists('products_post_type') ) {

// Register Custom Post Type
function products_post_type() {

    $labels = array(
        'name'                  => _x( 'Products', 'Post Type General Name', 'your_text_domain' ),
        'singular_name'         => _x( 'Product', 'Post Type Singular Name', 'your_text_domain' ),
        'menu_name'             => __( 'Products', 'your_text_domain' ),
        'name_admin_bar'        => __( 'Products', 'your_text_domain' ),
        'archives'              => __( 'Products Archives', 'your_text_domain' ),
        'attributes'            => __( 'Products Attributes', 'your_text_domain' ),
        'parent_item_colon'     => __( 'Parent Item:', 'your_text_domain' ),
        'all_items'             => __( 'All Items', 'your_text_domain' ),
        'add_new_item'          => __( 'Add New Item', 'your_text_domain' ),
        'add_new'               => __( 'Add New', 'your_text_domain' ),
        'new_item'              => __( 'New Item', 'your_text_domain' ),
        'edit_item'             => __( 'Edit Item', 'your_text_domain' ),
        'update_item'           => __( 'Update Item', 'your_text_domain' ),
        'view_item'             => __( 'View Item', 'your_text_domain' ),
        'view_items'            => __( 'View Items', 'your_text_domain' ),
        'search_items'          => __( 'Search Item', 'your_text_domain' ),
        'not_found'             => __( 'Not found', 'your_text_domain' ),
        'not_found_in_trash'    => __( 'Not found in Trash', 'your_text_domain' ),
        'featured_image'        => __( 'Featured Image', 'your_text_domain' ),
        'set_featured_image'    => __( 'Set featured image', 'your_text_domain' ),
        'remove_featured_image' => __( 'Remove featured image', 'your_text_domain' ),
        'use_featured_image'    => __( 'Use as featured image', 'your_text_domain' ),
        'insert_into_item'      => __( 'Insert into item', 'your_text_domain' ),
        'uploaded_to_this_item' => __( 'Uploaded to this item', 'your_text_domain' ),
        'items_list'            => __( 'Items list', 'your_text_domain' ),
        'items_list_navigation' => __( 'Items list navigation', 'your_text_domain' ),
        'filter_items_list'     => __( 'Filter items list', 'your_text_domain' ),
    );
    $args = array(
        'label'                 => __( 'Product', 'your_text_domain' ),
        'description'           => __( 'Products', 'your_text_domain' ),
        'labels'                => $labels,
        'supports'              => array( 'title', 'editor', 'thumbnail' ),
        'taxonomies'            => array( 'product_category' ),
        'hierarchical'          => false,
        'public'                => true,
        'show_ui'               => true,
        'show_in_menu'          => true,
        'menu_position'         => 5,
        'menu_icon'             => 'dashicons-products',
        'show_in_admin_bar'     => true,
        'show_in_nav_menus'     => true,
        'can_export'            => true,
        'has_archive'           => true,
        'exclude_from_search'   => false,
        'publicly_queryable'    => true,
        'capability_type'       => 'page',
        'show_in_rest'          => false,
    );
    register_post_type( 'product', $args );

}
add_action( 'init', 'products_post_type', 0 );

}

You should see a menu item in your Dashboard called Products.

It's possible too that you'll need to create a custom taxonomy for Products categories. Give this a try:

if ( ! function_exists( 'products_category' ) ) {

// Register Custom Taxonomy
function products_category() {

    $labels = array(
        'name'                       => _x( 'Product Categories', 'Taxonomy General Name', 'your_text_domain' ),
        'singular_name'              => _x( 'Product Category', 'Taxonomy Singular Name', 'your_text_domain' ),
        'menu_name'                  => __( 'Category', 'your_text_domain' ),
        'all_items'                  => __( 'All Items', 'your_text_domain' ),
        'parent_item'                => __( 'Parent Item', 'your_text_domain' ),
        'parent_item_colon'          => __( 'Parent Item:', 'your_text_domain' ),
        'new_item_name'              => __( 'New Item Name', 'your_text_domain' ),
        'add_new_item'               => __( 'Add New Item', 'your_text_domain' ),
        'edit_item'                  => __( 'Edit Item', 'your_text_domain' ),
        'update_item'                => __( 'Update Item', 'your_text_domain' ),
        'view_item'                  => __( 'View Item', 'your_text_domain' ),
        'separate_items_with_commas' => __( 'Separate items with commas', 'your_text_domain' ),
        'add_or_remove_items'        => __( 'Add or remove items', 'your_text_domain' ),
        'choose_from_most_used'      => __( 'Choose from the most used', 'your_text_domain' ),
        'popular_items'              => __( 'Popular Items', 'your_text_domain' ),
        'search_items'               => __( 'Search Items', 'your_text_domain' ),
        'not_found'                  => __( 'Not Found', 'your_text_domain' ),
        'no_terms'                   => __( 'No items', 'your_text_domain' ),
        'items_list'                 => __( 'Items list', 'your_text_domain' ),
        'items_list_navigation'      => __( 'Items list navigation', 'your_text_domain' ),
    );
    $args = array(
        'labels'                     => $labels,
        'hierarchical'               => true,
        'public'                     => true,
        'show_ui'                    => true,
        'show_admin_column'          => true,
        'show_in_nav_menus'          => true,
        'show_tagcloud'              => true,
        'show_in_rest'               => false,
    );
    register_taxonomy( 'product_category', array( 'products' ), $args );

}
add_action( 'init', 'products_category', 0 );

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM