多个重写规则链接PHP自定义文章

huangapple go评论80阅读模式
英文:

Multiple rewrite rules links php custom post

问题

我是新手学习 PHP,尝试编写一些重写规则来更改某些链接的结构,我尝试了一些我找到的代码,这些代码只适用于我在循环中指定的第一个自定义文章类型,我想要编写一个循环,可以更改我指定的所有自定义文章类型名称的链接(文章名称在数组中),这是我用于我的重写规则的代码:

function mycustomname_link($post_link, $post = 0) {
    
    if($post->post_name === $name_of_my_post) {
        
        return home_url('new' .'/'. 'posts'.'/'.$post->post_name.'/'.$post->ID . '/');
        
    }
    else{
        return $post_link;
    }
}

add_filter('post_type_link', 'mycustomname_link', 1, 3);

function mycustomname_rewrites_init(){
    add_rewrite_rule('new/posts/([^/]+)/([0-9]+)?$','index.php?post_type=nature_posts&p=$matches[1]&p=$matches[2]','top');
    flush_rewrite_rules();
}

add_action('init', 'mycustomname_rewrites_init');

我看到函数中有一个返回语句,因此它只在第一次迭代和第一个自定义文章名称上应用重写规则,你可以如何编写一个循环,以应用重写规则以适用于我将指定的所有文章名称,并且不会在第一次迭代时停止?

英文:

I'm new at php and I'm trying to do some rewrite rules to change the structure of some links, I tried some code I found and this code work but only for the first custom post type I indicated in the for loop, I want to do a for loop that change the links for all the custom post types names that I've indicated(the post names are in an array), here my code for my rewrite rule:

function mycustomname_link($post_link, $post = 0) {
    
    if($post->post_name === $name_of_my_post) {
        
        return home_url('new' .'/'. 'posts'.'/'. $post->post_name .'/'. $post->ID . '/');
        
    }
    else{
        return $post_link;
    }
}

add_filter('post_type_link', 'mycustomname_link', 1,3);

function mycustomname_rewrites_init(){
    add_rewrite_rule('new/posts/([^/]+)/([0-9]+)?$', 'index.php?post_type=nature_posts&p=$matches[1]&p=$matches[2]', 'top');
    flush_rewrite_rules();
}

add_action('init', 'mycustomname_rewrites_init');

I see that there is a return in the function so,it will apply the rewrite rules only at the first iteration and for the first custom post name, how can I do a for loop that will apply the rewrite rules for all the post_names that i will indicate and it will not stop at first iteration?

答案1

得分: 1

mycustomname_link函数中,我们添加了一个循环,遍历$custom_post_types数组。对于每个自定义文章类型,它检查当前文章类型是否与数组中的任何名称匹配。如果有匹配,将应用重写规则。

类似地,在mycustomname_rewrites_init函数中,我们添加了一个循环来注册数组中所有自定义文章类型的重写规则。每个自定义文章类型将有自己的重写规则。

通过这种修改,重写规则将适用于$custom_post_types数组中指定的所有自定义文章类型,不会在第一次迭代时停止。请确保使用您想要包括在重写规则中的所有自定义文章类型的名称更新$custom_post_types数组。

function mycustomname_link($post_link, $post = 0) {
    $custom_post_types = array('name_of_my_post', 'another_post_type', 'yet_another_post_type'); // 在这里添加所有自定义文章类型的名称

    foreach ($custom_post_types as $post_type) {
        if ($post->post_type === $post_type) {
            return home_url('new' . '/' . 'posts' . '/' . $post->post_name . '/' . $post->ID . '/');
        }
    }

    return $post_link;
}
add_filter('post_type_link', 'mycustomname_link', 10, 2);

function mycustomname_rewrites_init(){
    $custom_post_types = array('name_of_my_post', 'another_post_type', 'yet_another_post_type'); // 在这里添加所有自定义文章类型的名称

    foreach ($custom_post_types as $post_type) {
        add_rewrite_rule('new/posts/([^/]+)/([0-9]+)?$', 'index.php?post_type=' . $post_type . '&p=$matches[1]&p=$matches[2]', 'top');
    }

    flush_rewrite_rules();
}
add_action('init', 'mycustomname_rewrites_init');
英文:

You can try this,

In the mycustomname_link function, we added a loop that iterates through the $custom_post_types array. For each custom post type, it checks if the current post type matches any of the names in the array. If there is a match, the rewrite rule will be applied.

Similarly, in the mycustomname_rewrites_init function, we added a loop to register the rewrite rules for all the custom post types in the array. Each custom post type will have its own rewrite rule.

With this modification, the rewrite rules will be applied for all the custom post types specified in the $custom_post_types array, and it will not stop at the first iteration. Make sure to update the $custom_post_types array with the names of all the custom post types you want to include in the rewrite rules.

function mycustomname_link($post_link, $post = 0) {
    $custom_post_types = array('name_of_my_post', 'another_post_type', 'yet_another_post_type'); // Add all your custom post type names here

    foreach ($custom_post_types as $post_type) {
        if ($post->post_type === $post_type) {
            return home_url('new' .'/'. 'posts'.'/'. $post->post_name .'/'. $post->ID . '/');
        }
    }

    return $post_link;
}
add_filter('post_type_link', 'mycustomname_link', 10, 2);

function mycustomname_rewrites_init(){
    $custom_post_types = array('name_of_my_post', 'another_post_type', 'yet_another_post_type'); // Add all your custom post type names here

    foreach ($custom_post_types as $post_type) {
        add_rewrite_rule('new/posts/([^/]+)/([0-9]+)?$', 'index.php?post_type=' . $post_type . '&p=$matches[1]&p=$matches[2]', 'top');
    }

    flush_rewrite_rules();
}
add_action('init', 'mycustomname_rewrites_init');

huangapple
  • 本文由 发表于 2023年7月28日 03:14:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76782780.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定