admin管理员组文章数量:1278881
I know how to filter the output of the function the_permalink
- it is like this:
add_filter('the_permalink', 'my_the_permalink');
function my_the_permalink($url) {
return 'http://mysite/my-link/';
}
And it works when I use it like: <?PHP the_permalink($id); ?>
, but I wanted to change the link returned by get_permalink($id)
function. And this filter doesn't affect the returned permalink in that case.
I was trying to catch it with:
add_filter('post_link', 'my_get_permalink', 10, 3);
function my_get_permalink($url, $post, $leavename=false) {
return 'http://mysite/my-link/';
}
But this filter isn't fired for the get_permalink()
. So how can I alter the links returned by the get_permalink()
?
I know how to filter the output of the function the_permalink
- it is like this:
add_filter('the_permalink', 'my_the_permalink');
function my_the_permalink($url) {
return 'http://mysite/my-link/';
}
And it works when I use it like: <?PHP the_permalink($id); ?>
, but I wanted to change the link returned by get_permalink($id)
function. And this filter doesn't affect the returned permalink in that case.
I was trying to catch it with:
add_filter('post_link', 'my_get_permalink', 10, 3);
function my_get_permalink($url, $post, $leavename=false) {
return 'http://mysite/my-link/';
}
But this filter isn't fired for the get_permalink()
. So how can I alter the links returned by the get_permalink()
?
2 Answers
Reset to default 12Note that post_link
filter is only for the post
post type.
For other post types these filters are available:
post_type_link
for custom post typespage_link
for pageattachment_link
for attachment
The get_permalink()
function is actually a wrapper for:
get_post_permalink()
get_attachement_link()
get_page_link()
in those cases.
Here's a way (untested) to create a custom wpse_link
filter for all the above cases of get_permalink()
:
foreach( [ 'post', 'page', 'attachment', 'post_type' ] as $type )
{
add_filter( $type . '_link', function ( $url, $post_id, ? bool $sample = null ) use ( $type )
{
return apply_filters( 'wpse_link', $url, $post_id, $sample, $type );
}, 9999, 3 );
}
where we can now filter all cases with:
add_filter( 'wpse_link', function( $url, $post_id, $sample, $type )
{
return $url;
}, 10, 4 );
I successfully use this statement.
add_filter('post_type_link', function ($post_link, $post, $leavename, $sample) {
if ($post->post_type == 'mycustomposttype') {
...
$post_link = 'https://my.custom.site' . $some_uri;
}
return $post_link;
}, 999, 4);
本文标签: permalinksHow to filter to output of the getpermalink() function
版权声明:本文标题:permalinks - How to filter to output of the get_permalink() function 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741219830a2360755.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
get_my_permalink()
and use it instead ofget_permalink()
but I am wondering if I can do this on some higher level. – Picard Commented May 17, 2017 at 8:16