I decided to create custom post types in admin menu. So after browsing WP Codex and some forums online I have managed to do it.
I have added following code to achieve such a little task:
/**
* 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' );
I have managed to display posts from custom post type on my home page:
<?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; ?>
But there is a problem. WP says that "PAGE NOT FOUND" when I click on the post to view it. I have tried to create single-projects.php or archive-projects.php and nothing helps. What do I missing here?
You need to rebuild your rewrite rules.
In admin go to Settings → Permalinks and press Save Changes.
According to WordPress Codex, you need to manually run flush_rewrite_rules()
once manually for new custom post types.
Source: https://codex.wordpress.org/Function_Reference/flush_rewrite_rules