admin管理员组文章数量:1429633
I have a function that rewrites the URL before post slug including a custom taxonomy
function wpa_show_permalinks( $post_link, $post ){
if ( is_object( $post ) && $post->post_type == 'aktuelles' ){
$terms = wp_get_object_terms( $post->ID, 'nachrichtenkategorie' );
if( $terms ){
return str_replace( '%nachrichtenkategorie%' , $terms[0]->slug , $post_link );
}
}
return $post_link;
}
add_filter( 'post_type_link', 'wpa_show_permalinks', 1, 2 );
The function works just fine and replaces %nachrichtenkategorie%
in URL when accessing from the archive
Getting the permalink of a post with get_permalink(123)
doesn't replace the %nachrichtenkategorie%
in the url therefore gets into server error.
What am I doing wrong? Maybe the filter post_type_link
should be changed in something else that replaces that string in the url before displaying it. Any ideas? Thanks
I have a function that rewrites the URL before post slug including a custom taxonomy
function wpa_show_permalinks( $post_link, $post ){
if ( is_object( $post ) && $post->post_type == 'aktuelles' ){
$terms = wp_get_object_terms( $post->ID, 'nachrichtenkategorie' );
if( $terms ){
return str_replace( '%nachrichtenkategorie%' , $terms[0]->slug , $post_link );
}
}
return $post_link;
}
add_filter( 'post_type_link', 'wpa_show_permalinks', 1, 2 );
The function works just fine and replaces %nachrichtenkategorie%
in URL when accessing from the archive
Getting the permalink of a post with get_permalink(123)
doesn't replace the %nachrichtenkategorie%
in the url therefore gets into server error.
What am I doing wrong? Maybe the filter post_type_link
should be changed in something else that replaces that string in the url before displaying it. Any ideas? Thanks
1 Answer
Reset to default 0The code you are using looks valid, so…
Before going any further, check to see that the post you are testing with actually has the term you are expecting to find (and replace). In other words, verify your input is valid.
Also, you might consider just doing a straight string replace on the URL as, if the search term doesn't exist, no harm will be done. That said, if the search term does exist in the post name, for example, it will have unintended consequences so, you might also consider making the search more specific (like adding / (slash) to the search term).
Assuming the input is valid, you might also consider making this change in your rewrite rules (in apache or nginx) rather than in PHP. The reason is if you ever move to a new theme, for example, the functionality won't change (and it might even be faster than processing every request in PHP).
本文标签: Custom post type single with custom URL structure
版权声明:本文标题:Custom post type single with custom URL structure 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745479755a2660123.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
123
have some term fromnachrichtenkategorie
taxonomy? – nmr Commented May 20, 2019 at 9:34get_permalink
by post id if i'm not on the archive page gives this dead page andget_permalink
on article page gives this page that is good. So I'm trying to understand why. – Razvan Cuceu Commented May 20, 2019 at 12:46if( $terms ){ return str_replace( '%nachrichtenkategorie%' , $terms[0]->slug , $post_link ); }else{ return str_replace( '%nachrichtenkategorie%' , 'neuigkeiten' , $post_link ); }
Thank you. – Razvan Cuceu Commented May 20, 2019 at 13:42