如果更改永久链接的结构,则必须提供重写规则以了解新结构。此外,您没有正确构建附件永久链接。下一个代码正在运行:
add_filter( \'attachment_link\', \'wpd_attachment_link\', 20, 2 );
function wpd_attachment_link( $link, $attachment_id ){
$attachment = get_post( $attachment_id );
// Only for attachments actually attached to a parent post
if( ! empty( $attachment->post_parent ) ) {
$parent_link = get_permalink( $attachment->post_parent );
// make the link compatible with permalink settings with or without "/" at the end
$parent_link = rtrim( $parent_link, "/" );
$link = $parent_link . \'/gallery/\' . $attachment_id;
}
return $link;
}
add_action( \'init\', function() {
// Tell WordPress how to handle the new structure
add_rewrite_rule( \'(.+)/gallery/([0-9]{1,})/?$\', \'index.php?attachment_id=$matches[2]\', \'top\' );
} );
请记住在尝试此代码之前刷新重写规则(转到后端的permalink settgins页面并单击sabe按钮)。