[英]Adding slashes to custom Post Type slug in WP
I have a custom Post Type that gets created via XML-RPC, using RubyPress.我有一个使用 RubyPress 通过 XML-RPC 创建的自定义帖子类型。
When creating the post, I specify some slashes in the post_name (the slug, or permalink)创建帖子时,我在 post_name 中指定了一些斜杠(slug 或永久链接)
However, converts those slashes into hyphens:但是,将这些斜杠转换为连字符:
eg: year/code/some-string-name
ends up being year-code-some-string-name
例如:
year/code/some-string-name
最终成为year-code-some-string-name
Year and Code are dynamic values so I cannot use the parent-page approach, since each Post will have different values. Year 和 Code 是动态值,所以我不能使用父页面方法,因为每个 Post 都有不同的值。
After some research, this ended up working for me.经过一些研究,这最终对我有用。
You have to have installed a plugin called Custom Permalinks , as WordPress won't allow you to add slashes in the permalinks via code.您必须安装一个名为Custom Permalinks的插件,因为 WordPress 不允许您通过代码在永久链接中添加斜杠。
The following code will execute every time a MyPost is up Published.每次发布 MyPost 时,都会执行以下代码。 Besides Publish you can use other reserved words, look for "Post Status Transitions" if you're interested.
除了 Publish 之外,您还可以使用其他保留字,如果您有兴趣,请查找“Post Status Transitions”。
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.