admin管理员组文章数量:1420159
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 Answer
Reset to default 0Function 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;
}
本文标签: url rewritingAdd word to permalinks does not transform the urls
版权声明:本文标题:url rewriting - Add word to permalinks does not transform the urls 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745326106a2653599.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
%category%
to your permalink structure in Settings > Permalinks. – Jacob Peattie Commented Jul 12, 2019 at 9:48%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