开发者

WP: custom post type, rewrite url giving 404

I am registering a custom post type like so:

register_post_type('agent_info', array(
                'labels' => array('name' => __('Agents'), 
                           'singular_name' => __('Agent'),
                           'add_new_item' => __('Add 开发者_如何学PythonNew Agent'),
                           'edit_item' => __('Edit Agent'),
                     ),
                    'public' => true,
            '_builtin' => false,
            'query_var' => true,
            'rewrite' => array('slug' => 'agents', 'with_front' => false),
                    'show_ui' => true,
                    'supports' => array('title')
            ));

and when i try to access an agent using '/agents/agent-name-slug' i get a 404 error, but i change agents to agent_info, it works fine. What am i missing to allow the rewrite stuff to work properly?


Two things to check: are you hooking the post type registration onto init? and are you flushing the rewrite rules each time you change the rewrite slug?

If you're not hooking onto init, you should.

To flush rewrite rules, just go to the permalinks settings page. You don't even need to click save. It'll rewrite the rules as soon as you go there.

Also, I don't know if you realize this, but that won't give you a list view of your post type. To enable that, you have to add this also to your init hook:

add_rewrite_rule( 'agents/?$', 'index.php?post_type=agent_info', 'top' );

After adding that code, go back to the permalinks settings page.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜