找不到单一类型.php文件的页面

时间:2013-01-08 作者:strange man

我已经创建了自定义帖子类型“event”,以在名为single event的单个页面中显示。php文件,但它提供了404错误。我如何解决这个问题

flush\\u rewrite\\u rules();

请提供一些想法。。。。。。。

function custom_event() {
$labels = array( 
   \'name\' => _x(\'Event\', \'post type general name\'),
   \'singular_name\' => _x(\'Event\', \'post type singular name\'),
   \'add_new\' => _x(\'Add Event\', \'Content\'),
   \'add_new_item\' => __(\'Add New Event\'),
   \'edit_item\' => __(\'Edit Event\'),
   \'new_item\' => __(\'New Content\'),
   \'view_item\' => __(\'View Event\'),
   \'search_items\' => __(\'Search Content\'),
   \'not_found\' =>  __(\'Nothing found\'), 
   \'parent_item_colon\' => \'\',
   \'menu_name\' => __(\'Event\')
);

 $args = array(
\'labels\' => $labels,
\'public\' => true,
\'publicly_queryable\' => true,
\'show_ui\' => true, 
\'show_in_menu\' => true, 
\'query_var\' => true,
\'rewrite\' => true,
\'capability_type\' => \'post\',
\'has_archive\' => true, 
\'hierarchical\' => false,
\'menu_position\' => 20,
\'supports\' => array( \'title\', \'editor\')
 );

flush_rewrite_rules();
register_post_type(\'event\', $args);
}
  //$wp_rewrite->flush_rules();
  //add_action(\'admin_init\', \'flush_rewrite_rules\');
    add_action (\'init\', \'custom_event\');

3 个回复
最合适的回答,由SO网友:Rohit Pande 整理而成

跟随this 创建自定义帖子类型的教程。

以下是符合您要求的工作代码:

function create_post_type_event() {

    //Create custom post type for event

    $labels = array(
            \'name\' => _x(\'Event\', \'post type general name\'),
            \'singular_name\' => _x(\'Event\', \'post type singular name\'),
            \'add_new\' => _x(\'Add New\', \'event\'),
            \'add_new_item\' => __(\'Add New Event\'),
            \'edit_item\' => __(\'Edit Event\'),
            \'new_item\' => __(\'New Event\'),
            \'all_items\' => __(\'All Events\'),
            \'view_item\' => __(\'View Event\'),
            \'search_items\' => __(\'Search Event\'),
            \'not_found\' =>  __(\'No event found\'),
            \'not_found_in_trash\' => __(\'No event found in Trash\'),
            \'parent_item_colon\' => \'\',
            \'menu_name\' => __(\'Event\')

    );
    $args = array(
            \'labels\' => $labels,
            \'public\' => true,
            \'publicly_queryable\' => true,
            \'show_ui\' => true,
            \'show_in_menu\' => true,
            \'query_var\' => true,
            \'rewrite\' => TRUE,
            \'capability_type\' => \'post\',
            \'has_archive\' => true,
            \'hierarchical\' => false,
            \'menu_position\' => 5,
            \'supports\' => array( \'title\', \'editor\', \'author\', \'thumbnail\', \'excerpt\', \'comments\', \'custom-fields\' )
    );
    register_post_type(\'event\',$args);

    flush_rewrite_rules();          // Flush the rewrite rules and re-create the above for custom post types

}

add_action( \'init\', \'create_post_type_event\' );
创建此之后cpt 您可以创建single-event.php 文件,它应该可以正常工作。

SO网友:M-R

放置flush\\u rewrite\\u rules();注册调用后。此外,通过访问WP Admin->设置->永久链接->按保存按钮,尝试通过WP Admin刷新重写规则。

SO网友:RRikesh

尝试使用\'rewrite\' => array( \'slug\' => \'event\' ) 而不是\'rewrite\' => true.

此外,您不应该使用flush_rewrite_rules() 像这样。每次都会不必要地运行。您可以制作一个插件来定义自定义帖子类型,并使用register_activation_hook.

结束

相关推荐