Я искал и обнаружил, что он был разрешен в некоторых статьях с помощью flush_rewrite_rules(), но поскольку я уже использовал эту функцию, это не решило мою проблему.Custom PostType single page not working
Когда я создаю пост под 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');
Код, как вы его отправили, отлично отображает одну страницу. Я не получаю ошибку 404. Это может означать, что либо что-то вне этого кода вызывает проблему, либо вы смотрите на страницу с кешированной ошибкой. – NightHawk
Да нет проблемы с типом, но когда я изменяю свой тип с моим фактическим именем (головоломкой), одиночный дает ошибку. Но когда я устанавливаю slug = "game", он все равно отлично работает. Зачем? Как? «Головоломка» не может быть ссылочным словом или чем-то сломать что-либо. Я не уверен. Существует только одно соединение головоломки с системой, это метабоксическая сторона. Я не понимаю. – tolga
Продолжайте и обновите свой код выше до нерабочей версии, чтобы мы могли видеть, что не работает. – NightHawk