From trawling the support forum and blogs I see that a fair few people other than myself are having difficulties with custom post types and permalinks.
My custom post type (diary) registers ok and all appears fine on the admin side. If you search for it on the front end it appears in the search results. However I get a 404 when I click through to the diary post.
My code:
register_post_type( 'diary_post',
array(
'labels' => array(
'name' => __( 'Diary Post' ),
'singular_name' => __( 'Diary Post' ),
'add_new' => _x('Add New'),
'add_new_item' => __('Add New Diary Post'),
'edit_item' => __('Edit Diary Post'),
'new_item' => __('New Diary Post'),
'view_item' => __('View Diary Post'),
'search_items' => __('Search Diary Posts'),
'not_found' => __('No Diary posts found'),
'not_found_in_trash' => __('No Diary posts found in Trash'),
'parent_item_colon' => ''
),
'description' => __( 'Posts to appear on the Diary page' ),
'public' => true,
'publicly_queryable' => false,
'exclude_from_search' => false,
'query_var' => true,
'menu_position' => 4,
'supports' => array('title','editor','author','excerpt','thumbnail', 'custom-fields','comments','trackbacks','revisions'),
'taxonomies' => array( 'diary_post_type','post_tag'),
'has_archive' => true,
'rewrite' => array( 'slug' => 'diary','with_front' => false)
)
);
I have tried the following suggested solutions from various posts without any success:
- Re-saving my permalinks Changing
- 'rewrite' to just 'true' Both
- 'with_front' => false and
- 'with_front' => true Adding
- flush_rewrite_rules(); after
- register_post_type
- Checking there are no pages or posts named 'diary'
Even if I try to access a post via the default permalink structure such as http://localhost/?diary_post=my-开发者_C百科title-here I have no success.
My permalink structure for the site is currently /%year%/%postname%
- changing this to the default setting doesn't help either.
Any clues? At my wit's end here.
publicly_queryable
needs be true, but you probably don't need it (or exclude_from_search
) if you are setting public
. See the codex: http://codex.wordpress.org/Function_Reference/register_post_type#Arguments
Here's another answer to help people that may have the same problem as I did...
If you've recently created the new CPT, you may need to go into Settings > Permalinks and click Save Changes again. This allows any new rewrite rules to take effect.
You should add "_builtin" property to arguments.
function rw_portfolio_register(){
$args = array(
'label' => __('Portfolio'),
'singular_label' => __('Portfolio'),
'public' => true,
'show_ui' => true,
'_builtin' => false,
'capability_type' => 'post',
'hierarchical' => false,
'rewrite' => array("slug" => "project"),
'supports' => array('title', 'editor')//Boxes will be showed in the panel
);
register_post_type( 'rw_portfolio' , $args );
}
精彩评论