我有一个使用RubyPress通过XML-RPC创建的自定义帖子类型。
[创建帖子时,我在post_name中指定了一些斜线(子弹或永久链接)
但是,将那些斜杠转换为连字符:
例如:year/code/some-string-name
最终为year-code-some-string-name
年份和代码是动态值,所以我不能使用父页面方法,因为每个帖子将具有不同的值。
经过一番研究,最终对我有用。
您必须安装名为Custom Permalinks的插件,因为WordPress不允许您通过代码在永久链接中添加斜杠。
以下代码将在MyPost每次发布时执行。除了发布之外,您还可以使用其他保留字,如果您有兴趣,请查找“发布状态转换”。
add_action('publish_mypost', 'add_slashes_to_mypost_slug');
function add_slashes_to_mypost_slug( $post_id ) {
$post = get_post($post_id);
$slug = $post->post_name;
$slug_exploded = explode('-', $slug);
$year = array_shift($slug_exploded);
$code = array_shift($slug_exploded);
$remainder = implode('-', $slug_exploded);
$new_slug = $year.'/'.$code.'/'.$remainder;
update_post_meta($post_id, 'custom_permalink', $new_slug);
}