【问题标题】:Single post view for custom post type plugin自定义帖子类型插件的单个帖子视图
【发布时间】:2016-05-14 20:17:30
【问题描述】:

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

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。仍然无法预览我的“意见”帖子类型的内容(通过上面的自定义插件代码引入)。

请有人帮我解决这个问题。

【问题讨论】:

  • 您是否刷新了“设置”>“永久链接”下的永久链接,然后按“保存更改”?

标签: wordpress custom-post-type


【解决方案1】:

来自 WordPress codex:

WordPress 保留所有自定义重写规则的缓存。有时插件 或主题对这些规则进行修改,但是 WordPress 会 在重新生成缓存之前不会真正识别更改。

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

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

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

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

【讨论】:

    【解决方案2】:

    作为一个插件,需要用到函数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 的建议对于主题内的自定义帖子类型很有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-11
      • 2021-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多