最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

url rewriting - Add word to permalinks does not transform the urls

programmeradmin1浏览0评论

Let's say I want to add the permalink for the URLs: site/section/{postname} and the 'section' matches the name of one post's category (just for one case). After having a moment for study the documentation. I added a function for the rewrite_url. In the 'functions.php' file of my template (which is Astra). For what I add at the end:

add_action( 'init', 'wpa_rewriterules' ); 

function wpa_rewriterules() 
{
add_rewrite_rule( 
    // The regex to match the incoming URL 
    'section/([^/]+)/?', 
    // The resulting internal URL: `index.php` because we still use WordPress 
    'index.php?name=$matches[1]', 
    'top' ); 

}  

I have also read that I would have to add a filter to 'the_permalink' in the same way, so that each time a link has to be displayed it will be transformed to the desired URL. So I added the code for that filter also in the same 'functions.php' file:

add_filter('the_permalink', 'post_permalink_w_seccion'); 

function post_permalink_w_seccion( $link ) { 
    global $post; 
    $postcat = get_the_category( $post->ID ); 
    if ( $post->post_type === 'post' && $postcat->slug == 'section') { 

        $link = str_replace( $post->post_name, 'section/' . $post->post_name,  get_permalink( $post ) ); 
    } 
    return $link;  
} 

but this is what does not work for me. The urls do not transform or change. Can you help me to see what I'm doing wrong or what I'm missing?

Also, I'm not sure if this is the best way to do what I want. I'm new to the Wordpress world.

P.S. I have always saved changes to the 'permalinks' doing a flush of the rules of URLs.

Thank you.

Let's say I want to add the permalink for the URLs: site/section/{postname} and the 'section' matches the name of one post's category (just for one case). After having a moment for study the documentation. I added a function for the rewrite_url. In the 'functions.php' file of my template (which is Astra). For what I add at the end:

add_action( 'init', 'wpa_rewriterules' ); 

function wpa_rewriterules() 
{
add_rewrite_rule( 
    // The regex to match the incoming URL 
    'section/([^/]+)/?', 
    // The resulting internal URL: `index.php` because we still use WordPress 
    'index.php?name=$matches[1]', 
    'top' ); 

}  

I have also read that I would have to add a filter to 'the_permalink' in the same way, so that each time a link has to be displayed it will be transformed to the desired URL. So I added the code for that filter also in the same 'functions.php' file:

add_filter('the_permalink', 'post_permalink_w_seccion'); 

function post_permalink_w_seccion( $link ) { 
    global $post; 
    $postcat = get_the_category( $post->ID ); 
    if ( $post->post_type === 'post' && $postcat->slug == 'section') { 

        $link = str_replace( $post->post_name, 'section/' . $post->post_name,  get_permalink( $post ) ); 
    } 
    return $link;  
} 

but this is what does not work for me. The urls do not transform or change. Can you help me to see what I'm doing wrong or what I'm missing?

Also, I'm not sure if this is the best way to do what I want. I'm new to the Wordpress world.

P.S. I have always saved changes to the 'permalinks' doing a flush of the rules of URLs.

Thank you.

Share Improve this question edited Jul 12, 2019 at 15:12 MikeBau asked Jul 12, 2019 at 9:45 MikeBauMikeBau 133 bronze badges 2
  • 1 You can include the category in the permalink without any code, just add %category% to your permalink structure in Settings > Permalinks. – Jacob Peattie Commented Jul 12, 2019 at 9:48
  • I know. But what I need is not for all cases. so you cannot configurate it selecting the option %categoryname%%postname% from Settings > Permalinks. I would need to know what I have wrong in the filter for construction of the links works fine and shows the prefix 'section' added to them. – MikeBau Commented Jul 12, 2019 at 14:02
Add a comment  | 

1 Answer 1

Reset to default 0

Function get_the_category() returns array, that is why the condition $postcat->slug == 'section' is always false.

In its current form, your filter can change links to posts that do not have the section category. You should change the function to check the post for which get_permalink() was called, instead of checking the global $post.

add_filter( 'post_link', 'post_permalink_w_section', 10, 2 ); 

function post_permalink_w_section( $link, $post )
{ 
    if ( $post->post_type === 'post' && has_category('section', $post) )
    {
        $link = str_replace( $post->post_name, 'section/' . $post->post_name, $link ); 
    } 
    return $link;  
} 
发布评论

评论列表(0)

  1. 暂无评论