admin管理员组文章数量:1336115
I want to customize the output of the permalink for a custom post type, but I want the permalink to still be editable when editing a post. I'm using the post_type_link
filter:
add_filter( 'post_type_link', function( $post_link, $post, $leavename, $sample ) {
if ( 'job' == $post->post_type ) {
$post_link = 'http://localhost:8888/testsite/job/' . $post->post_name . '/';
}
return $post_link;
}, 10, 4 );
When this filter is active, it no longer shows the "Edit" button next to the permalink when editing the post. Even if I remove the custom permalink stuff and only have return $post_link;
it still disables editing of the permalink.
How can I customize the output and still keep the permalink editable?
I want to customize the output of the permalink for a custom post type, but I want the permalink to still be editable when editing a post. I'm using the post_type_link
filter:
add_filter( 'post_type_link', function( $post_link, $post, $leavename, $sample ) {
if ( 'job' == $post->post_type ) {
$post_link = 'http://localhost:8888/testsite/job/' . $post->post_name . '/';
}
return $post_link;
}, 10, 4 );
When this filter is active, it no longer shows the "Edit" button next to the permalink when editing the post. Even if I remove the custom permalink stuff and only have return $post_link;
it still disables editing of the permalink.
How can I customize the output and still keep the permalink editable?
Share Improve this question asked May 24, 2020 at 10:41 GavinGavin 4247 silver badges21 bronze badges 2 |1 Answer
Reset to default 5How can I customize the output and still keep the permalink editable?
The following worked for me:
$post_link = home_url( '/job/' . ( $leavename ? '%postname%' : $post->post_name ) . '/' );
I.e. If the $leavename
is true
, use %postname%
in the URL. Otherwise, you may then use the actual post name/slug, i.e. the value of the $post->post_name
.
Additionally, I suggest you to use home_url()
than hard-coding the site URL into the permalink. :)
And I presume you will or have already setup the rewrite rules for the permalinks (/job/<post slug>/
in the above example)?
Even if I remove the custom permalink stuff and only have
return $post_link;
it still disables editing of the permalink.
I'm not getting that issue, so it's probably a theme/plugin conflict on your site — try deactivating plugins and enable them back one at a time until you've found the culprit.
本文标签: Permalink slug no longer editable when using posttypelink filter
版权声明:本文标题:Permalink slug no longer editable when using post_type_link filter 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742403288a2468314.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
job
, then the default permalink structure should bejob/<post slug>
, so why are you manually customizing it via that filter? – Sally CJ Commented May 24, 2020 at 11:12