我决定创建一个插件,而不是将我的自定义帖子类型放入我的主题中(因为我的主题将来可能会改变)。
我为我的插件创建了一个文件夹并创建了一个文件(myplugin.php),其内容如下:
<?php
/*
Plugin Name: myplugin
Plugin URI: http://www.mywebsite.com
Description: Custom post types for my website
Version: 1.0
Author: my name
Author URI: http://www.mywebsite.com
License: Private
*/
?>
并将我的自定义帖子类型添加到 functions.php 文件中,如下所示:
<?php
add_action( 'init', 'register_cpt_portfolio' );
function register_cpt_portfolio() {
$labels = array(
'name' => _x( 'Portfolio', 'portfolio' ),
'singular_name' => _x( 'Portfolio', 'portfolio' ),
'add_new' => _x( 'Add New', 'portfolio' ),
'add_new_item' => _x( 'Add New Portfolio', 'portfolio' ),
'edit_item' => _x( 'Edit Portfolio', 'portfolio' ),
'new_item' => _x( 'New Portfolio', 'portfolio' ),
'view_item' => _x( 'View Portfolio', 'portfolio' ),
'search_items' => _x( 'Search Portfolio', 'portfolio' ),
'not_found' => _x( 'No portfolio found', 'portfolio' ),
'not_found_in_trash' => _x( 'No portfolio found in Trash', 'portfolio' ),
'parent_item_colon' => _x( 'Parent Portfolio:', 'portfolio' ),
'menu_name' => _x( 'Portfolio', 'portfolio' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'description' => 'To display completed works.',
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'custom-fields' ),
'taxonomies' => array( 'category' ),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => true,
'query_var' => true,
'can_export' => true,
'rewrite' => true,
'capability_type' => 'page'
);
register_post_type( 'portfolio', $args );
}
?>
但是,激活插件后,我没有看到我的自定义帖子类型 - 我错过了这里的任何步骤吗?谢谢你的帮助!