我决定在管理菜单中创建自定义帖子类型。因此,在浏览WP Codex和一些在线论坛后,我设法做到了。
我添加了以下代码来实现这样一个小任务:
/**
* Register a book post type.
*
* @link http://codex.wordpress.org/Function_Reference/register_post_type
*/
function codex_book_init() {
$labels = array(
'name' => _x( 'Projects', 'post type general name', 'your-plugin-textdomain' ),
'singular_name' => _x( 'Projects', 'post type singular name', 'your-plugin-textdomain' ),
'menu_name' => _x( 'Projects', 'admin menu', 'your-plugin-textdomain' ),
'name_admin_bar' => _x( 'Project', 'add new on admin bar', 'your-plugin-textdomain' ),
'add_new' => _x( 'Add New', 'project', 'your-plugin-textdomain' ),
'add_new_item' => __( 'Add New Project', 'your-plugin-textdomain' ),
'new_item' => __( 'New Project', 'your-plugin-textdomain' ),
'edit_item' => __( 'Edit Project', 'your-plugin-textdomain' ),
'view_item' => __( 'View Project', 'your-plugin-textdomain' ),
'all_items' => __( 'All Projects', 'your-plugin-textdomain' ),
'search_items' => __( 'Search Project', 'your-plugin-textdomain' ),
'parent_item_colon' => __( 'Parent Project:', 'your-plugin-textdomain' ),
'not_found' => __( 'No projects found.', 'your-plugin-textdomain' ),
'not_found_in_trash' => __( 'No projects found in Trash.', 'your-plugin-textdomain' )
);
$args = array(
'labels' => $labels,
'description' => __( 'Description.', 'your-plugin-textdomain' ),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_admin_bar' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'projects' ),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_icon' => 'dashicons-images-alt2',
'menu_position' => 5,
'supports' => array(
'title',
'editor',
'excerpt',
'trackbacks',
'custom-fields',
'comments',
'revisions',
'thumbnail',
'author',
'page-attributes')
);
register_post_type( 'projects', $args );
}
add_action( 'init', 'codex_book_init' );
function create_book_tax() {
register_taxonomy(
'project-types',
'projects',
array(
'label' => __( 'Project Types' ),
'rewrite' => array( 'slug' => 'project-types' ),
'hierarchical' => true,
)
);
}
add_action( 'init', 'create_book_tax' );
我已设法在我的主页上显示自定义帖子类型的帖子:
<?php query_posts('post_type=projects');
while (have_posts()) : the_post();
?>
<?php if ( has_post_thumbnail() ) : ?>
<div class="project-module">
<a class="project-thumbnail" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail(); ?>
<div class="project-module-title">
<?php the_title(); ?>
</div>
</a>
</div>
<?php endif; ?>
<?php endwhile; ?>
但是有一个问题。当我点击帖子查看它时,WP说“页面未找到”。 我试图创建single-projects.php或archive-projects.php并没有任何帮助。 我在这里缺少什么?
答案 0 :(得分:2)
答案 1 :(得分:1)
根据WordPress Codex,您需要手动为新的自定义帖子类型手动运行flush_rewrite_rules()
。
来源:https://codex.wordpress.org/Function_Reference/flush_rewrite_rules