自定义帖子类型插件的单个帖子视图

时间:2016-05-13 13:53:04

标签: wordpress wordpress-plugin custom-post-type

我创建了自定义帖子类型"意见"插入。我的代码如下:

function create_post_type_opinion() {
register_post_type('opinion', 
    array(
        'labels' => array(
            'name' => __( 'Opinions'),
                    'singular_name' => __( 'Opinion'),
            'add_new' => __('Add New Opinion' ),
            'add_new_item' => __('Add New Opinion'),
            'edit' => __( 'Edit Opinion' ),
                'edit_item' => __( 'Edit Opinion' ),
                'new_item' => __( 'New Opinion' ),
                'view' => __( 'View Opinion' ),
                'view_item' => __( 'View Opinion' ),
                'search_items' => __( 'Search Opinions' ),
                'not_found' => __( 'No Opinions found' ),
                'not_found_in_trash' => __( 'No Opinions found in Trash' )
                    ),
    'public' => true,
    'menu_position' => 5,
    'menu_icon' => plugins_url( 'images/opinion-20x20.png', __FILE__ ),
    'rewrite' => array(
        'slug' => __('opinion')
    ), 
    'supports' => array( 'title','editor', 'excerpt', 'comments', 'revisions', 'thumbnail')));
}
add_action( 'init', 'create_post_type_opinion' );

但我无法预览自定义帖子。它调用index.php的内容。我在我的主题文件夹中创建了single-opinion.php。仍然无法预览我的观点和#34;的内容。帖子类型(通过上面的自定义插件代码介绍)。

请帮助我解决这个问题。

2 个答案:

答案 0 :(得分:0)

来自WordPress codex

  

WordPress保留所有自定义重写规则的缓存。有时插件   或者主题对这些规则进行修改,但WordPress会   在重新生成缓存之前,实际上并未识别出这些更改。

转到“设置 - >永久链接”并选中“帖子名称”作为网站网址结构并保存设置以刷新重写规则。

如果上述方法不起作用,请尝试通过以下代码刷新重写规则:

function mytheme_rewrite_flush() {
    flush_rewrite_rules();
}
add_action( 'after_switch_theme', 'mytheme_rewrite_flush' );

https://codex.wordpress.org/Function_Reference/flush_rewrite_rules

答案 1 :(得分:0)

作为一个插件,需要使用函数register_activation_hook:

function create_post_type_opinion(){
register_post_type('opinion', 
array(
    'labels' => array(
        'name' => __( 'Opinions'),
                'singular_name' => __( 'Opinion'),
        'add_new' => __('Add New Opinion' ),
        'add_new_item' => __('Add New Opinion'),
        'edit' => __( 'Edit Opinion' ),
            'edit_item' => __( 'Edit Opinion' ),
            'new_item' => __( 'New Opinion' ),
            'view' => __( 'View Opinion' ),
            'view_item' => __( 'View Opinion' ),
            'search_items' => __( 'Search Opinions' ),
            'not_found' => __( 'No Opinions found' ),
            'not_found_in_trash' => __( 'No Opinions found in Trash' )
                ),
'public' => true,
'menu_position' => 5,
'menu_icon' => plugins_url( 'images/opinion-20x20.png', __FILE__ ),
'rewrite' => array(
    'slug' => __('opinion')
), 
'supports' => array( 'title','editor', 'excerpt', 'comments', 'revisions', 'thumbnail')));
}
//runs only when the theme is set up
function custom_flush_rules(){
//defines the post type so the rules can be flushed.
create_post_type_opinion();

//and flush the rules.
flush_rewrite_rules();
}
register_activation_hook(__FILE__, 'custom_flush_rules');
add_action('init', 'create_post_type_opinion');

ucheng的建议适用于主题内的自定义帖子类型。