我搜索并发现它在flush_rewrite_rules()的帮助下在一些文章中得到了解决,但由于我已经使用了该函数,因此无法解决我的问题。
当我在mytype下创建帖子,然后点击"查看帖子"时,它会重定向到404。
我用" mytype"替换了我的帖子类型只是为了更容易阅读。
谢谢你们。
add_action( 'init', 'codex_puzzle_init' );
/**
* Register a puzzle post type.
*
* @link http://codex.wordpress.org/Function_Reference/register_post_type
*/
function codex_puzzle_init() {
$labels = array(
'name' => _x( 'Puzzles', 'post type general name', 'domain' ),
'singular_name' => _x( 'Puzzles', 'post type singular name', 'domain' ),
'menu_name' => _x( 'Puzzles', 'admin menu', 'domain' ),
'name_admin_bar' => _x( 'Puzzle', 'add new on admin bar', 'domain' ),
'add_new' => _x( 'Add New', 'puzzle', 'domain' ),
'add_new_item' => __( 'Add New Puzzle', 'domain' ),
'new_item' => __( 'New Puzzle', 'domain' ),
'edit_item' => __( 'Edit Puzzle', 'domain' ),
'view_item' => __( 'View Puzzle', 'domain' ),
'all_items' => __( 'All Puzzles', 'domain' ),
'search_items' => __( 'Search Puzzle', 'domain' ),
'parent_item_colon' => __( 'Parent Puzzle:', 'domain' ),
'not_found' => __( 'No Puzzles found.', 'domain' ),
'not_found_in_trash' => __( 'No Puzzles found in Trash.', 'domain' )
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'puzzle' ),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => 4,
'menu_icon' => 'dashicons-tablet',
'supports' => array( 'title', 'editor', 'thumbnail', 'puzzle-category'),
'taxonomies' => array('puzzle-category')
);
register_post_type( 'puzzle', $args );
flush_rewrite_rules();
}
add_filter( 'post_updated_messages', 'codex_puzzle_updated_messages' );
/**
* puzzles update messages.
*
* See /wp-admin/edit-form-advanced.php
*
* @param array $messages Existing post update messages.
*
* @return array Amended post update messages with new CPT update messages.
*/
function codex_puzzle_updated_messages( $messages ) {
$post = get_post();
$post_type = get_post_type( $post );
$post_type_object = get_post_type_object( $post_type );
$messages['puzzle'] = array(
0 => '', // Unused. Messages start at index 1.
1 => __( 'puzzles updated.', 'domain' ),
2 => __( 'Custom field updated.', 'domain' ),
3 => __( 'Custom field deleted.', 'domain' ),
4 => __( 'puzzle updated.', 'domain' ),
/* translators: %s: date and time of the revision */
5 => isset( $_GET['revision'] ) ? sprintf( __( 'puzzle restored to revision from %s', 'domain' ), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
6 => __( 'puzzle published.', 'domain' ),
7 => __( 'puzzle saved.', 'domain' ),
8 => __( 'puzzle submitted.', 'domain' ),
9 => sprintf(
__( 'puzzle scheduled for: <strong>%1$s</strong>.', 'domain' ),
// translators: Publish box date format, see http://php.net/date
date_i18n( __( 'M j, Y @ G:i', 'domain' ), strtotime( $post->post_date ) )
),
10 => __( 'puzzle draft updated.', 'domain' )
);
if ( $post_type_object->publicly_queryable ) {
$permalink = get_permalink( $post->ID );
$view_link = sprintf( ' <a href="%s">%s</a>', esc_url( $permalink ), __( 'View puzzle', 'domain' ) );
if(isset($messages[ $post_type ])){
if($post_type == "puzzle"){
$messages[ $post_type ][1] .= $view_link;
$messages[ $post_type ][6] .= $view_link;
$messages[ $post_type ][9] .= $view_link;
$preview_permalink = add_query_arg( 'preview', 'true', $permalink );
$preview_link = sprintf( ' <a target="_blank" href="%s">%s</a>', esc_url( $preview_permalink ), __( 'Preview puzzle', 'domain' ) );
$messages[ $post_type ][8] .= $preview_link;
$messages[ $post_type ][10] .= $preview_link;
}
}
}
return $messages;
}
function register_puzzle_category() {
register_taxonomy(
'puzzle-category',
'puzzle',
array(
'hierarchical' => true,
'label' => _("Puzzle Category", "domain"), //Display name
'query_var' => true,
'rewrite' => array(
'slug' => 'puzzle-category', // This controls the base slug that will display before each term
'with_front' => false // Don't display the category base before
)
)
);
}
add_action( 'init', 'register_puzzle_category');
答案 0 :(得分:0)
替换行
'rewrite'=&gt;数组('slug'=&gt;'puzzle')到
'rewrite'=&gt; register_post_type函数中的数组('slug'=&gt;'puzzle','with_front'=&gt; false)。