Answer the question
In order to leave comments, you need to log in
How to make CNC for sitename/?attachment_id=1 in Wordpress?
There are pages of attached files, they build links sitename.com/?attachment_id=1 in such a way, where 1 is the id of the attached file.
It is necessary that instead of such a link there should be a link like sitename.com/%name of the attached file%
Do not ask why this is necessary, I myself am at a loss.
For all other links, the NC is configured, but I can’t figure out exactly what to do with the page of the attached file ...
Answer the question
In order to leave comments, you need to log in
In general, I made a crutch yesterday. In /wp-includes/link-template.php in the last if of the get_attachment_link function added $link = home_url( 'attachment/' . $object->post_name );
Now everything works almost as intended, but instead of sitename/%name of the attached file% there is sitename/attachment/%name of the attached file%
It would be possible to remove /attachment/ through .htaccess, but so that later there would be no conflict if there is a post and a file with the same name, decided to leave attachment.
True, as soon as Wordpress is updated, everything will fly off, if anyone has a more beautiful solution, I would be glad to see it.
This is what you can add to functions.php
add_action('generate_rewrite_rules', 'attachment_rewrite_rule_14924');
function attachment_rewrite_rule_14924($wp_rewrite){
$new_rules = array();
$new_rules['attachment/(\d*)$'] = 'index.php?attachment_id=$matches[1]';
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question