Lee*_*Lee 5 php wordpress permalinks custom-post-type
我已经完全重写了这个问题,因为它有点长,我担心人们会在没有完全阅读的情况下跳过它.
我有一个自定义帖子类型(过程),其中包含一个自定义元键/值以及我想用作slug的页面ID.
我正在使用此功能(如下所示)在管理区域中创建永久链接,但在查看这些时,页面是404错误.如何创建重写规则以使用相同的格式?
function bv_procedure_parent_slug( $url, $post ) {
if( get_post_type( $post ) == 'procedure' && get_post_meta( $post->ID, 'procedure_parent', true ) ) {
$procedure_parent = get_post( get_post_meta( $post->ID, 'procedure_parent', true))->post_name;
if( $procedure_parent ) {
$url = str_replace( 'procedure', $procedure_parent, $url );
}
}
return $url;
}
add_filter( 'post_type_link', 'bv_procedure_parent_slug', 1, 3 );
Run Code Online (Sandbox Code Playgroud)
这里的目标是我将在这里有很多帖子,其中包含元键/值procedure_parent => 31(其中31是页面ID,帖子名称是'face').查看单个帖子时,而不是/procedure/facelift/我希望的URL /face/facelift/.
为此,我相信我需要能够在创建重写规则时访问$ post,以便我可以使用get_post_meta().
但是怎么样?
看来你的问题有点过于复杂了。如果您执行以下操作,它会起作用吗?
http://example.com/custompostname/%category%/%postname%/.否则我认为add_permastruct()这是你缺少的功能。我正在理论化这一点,查看https://wordpress.stackexchange.com/a/253325/58884上类似问题的解决方案。一些代码是这样的:
function my_custom_rewrite() {
add_permastruct('procedure', '/%parent%/', false);
}
add_action('init', 'my_custom_rewrite');
Run Code Online (Sandbox Code Playgroud)
而不是'rewrite' => array( 'slug' => '%parent%', 'with_front' => false )在后类型声明中。该声明的部分'slug' => '%parent%'是自定义帖子类型名称的可选替换($post_type在register_post_type() 法典条目中)。因此,如果您有,'rewrite' => array( 'slug' => 'wibblewobble', 'with_front' => true)您的网址将从 更改http://example.com/procedure/%postname%/为http://example.com/wibblewobble/%postname%/。'with_front' => false我不认为它有任何作用。当您输入时,'slug' => '%parent%'我认为 WordPress 会将其视为字符串,并且不会动态地对其执行任何操作。
也一定要使用flush_rewrite_rules();。您可以在测试时在挂钩函数中使用它init,然后在工作后将其移至主题/插件激活挂钩函数。
祝你好运!