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 badges
Add a comment  | 

1 Answer 1

Reset to default 0

You 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