admin管理员组文章数量:1200771
Am trying to add custom path to my article posttype
so my article path will be www.example/parent-post-1/parent-post-2/the-actual-post
i tried to add this code
/**
* @param $post_link
* @param $id
* Добавить название категории продукта к ссылке
* @return array|mixed|string|string[]
*/
function wpa_course_post_link($post_link, $id = 0)
{
$post = get_post($id);
if ($post instanceof WP_Post) {
$parent = get_field('parents', $post->ID) ?: [];
$link = '';
foreach ($parent as $post) {
$link .= $post->post_name . '/';
}
if ($parent) {
$post_link = str_replace('%parents%/', $link, $post_link);
}
}
return $post_link;
}
add_filter('post_type_link', 'wpa_course_post_link', 1, 3);
its works if the post has only 1 parent post, but if the post has more than 1 parent posts, it shows 404
my CPT UI settings. Custom rewrite slug -> articles/%parents%
how to solve this? thank you
Am trying to add custom path to my article posttype
so my article path will be www.example/parent-post-1/parent-post-2/the-actual-post
i tried to add this code
/**
* @param $post_link
* @param $id
* Добавить название категории продукта к ссылке
* @return array|mixed|string|string[]
*/
function wpa_course_post_link($post_link, $id = 0)
{
$post = get_post($id);
if ($post instanceof WP_Post) {
$parent = get_field('parents', $post->ID) ?: [];
$link = '';
foreach ($parent as $post) {
$link .= $post->post_name . '/';
}
if ($parent) {
$post_link = str_replace('%parents%/', $link, $post_link);
}
}
return $post_link;
}
add_filter('post_type_link', 'wpa_course_post_link', 1, 3);
its works if the post has only 1 parent post, but if the post has more than 1 parent posts, it shows 404
my CPT UI settings. Custom rewrite slug -> articles/%parents%
how to solve this? thank you
Share Improve this question asked May 29, 2022 at 9:37 Мохамед РуслановичМохамед Русланович 53 bronze badges1 Answer
Reset to default 0You shouldn't need to manually handle this with hooks & custom slug settings - just ensure hierarchical
is set to true
in your register_post_type
arguments.
You mention "CPT UI" settings, are you referring to the Custom Post Type UI plugin? If so, make sure Hierarchical is set to True in the Settings metabox.
本文标签: url rewritingTrying to add array of paths to post permalink
版权声明:本文标题:url rewriting - Trying to add array of paths to post permalink 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738566716a2100292.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论