在Wordpress Functions.php中创建和显示自定义帖子类型

时间:2019-06-01 01:13:16

标签: php wordpress

阅读http://shop.oreilly.com/product/0636920027508.do这本书,我在第511页上,我们在PHP中定义了一个自定义帖子类型。使用下面的代码,它应该在页面上显示创建的帖子类型:“产品”。

我的不是。

该代码是否准确或我做错了什么?

 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');

这本书已有五岁了,我现在正在使用最新版本的WordPress。您认为这是问题所在吗?

1 个答案:

答案 0 :(得分:0)

您可能缺少“ register_post_type”的某些强制密钥。您可以在此处查看完整的文档:https://developer.wordpress.org/reference/functions/register_post_type/

(我建议您使用Wordpress Codex和Developer Code Reference而不是脱机书籍。信息会随着版本的变化而变化,这些都是保持更新的地方):

尝试以下代码:

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 );

}

您应该在控制台中看到一个名为“产品”的菜单项。

也可能需要为“产品”类别创建自定义分类法。试试看:

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 );

}