$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>url rewriting - Overwrite rewrite-slug of built in post-type 'post'|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

url rewriting - Overwrite rewrite-slug of built in post-type 'post'

matteradmin9PV0评论

I need to change the rewrite-slug of the built in post-type 'post' to 'example/magazine/news/any-nice-news-item'.

When I re-register the post-type 'post' and set the rewrite-slug to 'magazine/news' it works fine. But now I'm not sure about any side-effects. Does anybody have any positive or negative experience with that?

Here is the code:

register_post_type( 'post', array(
    'labels' => array(
        'name_admin_bar' => _x( 'Post', 'add new on admin bar' ),
    ),
    'public'  => true,
    /* internal use only. don't use this when registering your own post type. */
    '_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
    'capability_type' => 'post',
    'map_meta_cap' => true,
    'hierarchical' => false,
    'rewrite' => array('slug' => 'magazine/news'),
    'query_var' => false,
    'delete_with_user' => true,
    'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'post-formats' ),
) );

I need to change the rewrite-slug of the built in post-type 'post' to 'example/magazine/news/any-nice-news-item'.

When I re-register the post-type 'post' and set the rewrite-slug to 'magazine/news' it works fine. But now I'm not sure about any side-effects. Does anybody have any positive or negative experience with that?

Here is the code:

register_post_type( 'post', array(
    'labels' => array(
        'name_admin_bar' => _x( 'Post', 'add new on admin bar' ),
    ),
    'public'  => true,
    /* internal use only. don't use this when registering your own post type. */
    '_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
    'capability_type' => 'post',
    'map_meta_cap' => true,
    'hierarchical' => false,
    'rewrite' => array('slug' => 'magazine/news'),
    'query_var' => false,
    'delete_with_user' => true,
    'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'post-formats' ),
) );
Share Improve this question asked Oct 30, 2013 at 18:41 Matthias LohscheidtMatthias Lohscheidt 511 silver badge3 bronze badges 1
  • What the propose of re-register post type post ? – Oleg Butuzov Commented Oct 30, 2013 at 19:50
Add a comment  | 

3 Answers 3

Reset to default 3

You do not need to re register post type.

Symply go in you backend, Settings > Permalinks and set the permalink structure to a custom one: /magazine/news/%postname%/ then save changes.

See image below:


I wouldn't recommend reregistering the 'posts' post-type. Modifying _builtins can be a headache.

It may be best to simply change the "category Permalink" under Settings > Permalinks to:

'magazine/news/'

Or to create a subcategory named "news" with a parent category named "magazine", and use something like the "WP Htaccess Control" plugin to strip out the "category/" portion of the slug from posts.

The options above re-write every URL to be under that slug. You need to hook in with the registered_post_type to adjust it "solely" for the 'post' post type.

Post a comment

comment list (0)

  1. No comments so far