I want to change the output of the_permalink()
function and the likes. So, I don't want to change the mermalink structure for all the post types, only the 'post' post type, and only the single page.
I already changed the rewrite rules so that
mysite.com/news/2016/the-news-title
will drive the user to the single post page template and display content properly. Fine.
Now, as I said, I want the_permalink()
function to generate links with that structure.
How could I do it?
Ok following Noman indications, I can post my own answer. I could the 'the_permalink' filter and modify the output if post_type == 'post'. The basic code:
add_filter('the_permalink', 'edit_the_permalink', 10, 2);
function edit_the_permalink($permalink, $post) {
// you should pass the post object when calling the_permalink()
if(is_object($post) && $post->post_type == 'post'){
// [...do the magic...]
return $permalink;
}
}
Please note that both the_permalink()
and get_permalink()
(alias get_the_permalink()
) accept the $post argument and you should pass it as a post object.
Also, note that you can do pretty much the same with the post_type_link
filter if you want modify the custom post types permalink generation (output of get_post_permalink()
). In this case the callback will receive 4 parameters: $post_link, $post, $leavename, $sample.