function ns_add_rewrite_rules($rules){
$dirname = 'media';
$custom_rules = array("{$dirname}/(.?.+?)/?$" => 'index.php?attachment=$matches[1]');
return array_merge($custom_rules, $rules);
}
add_filter('rewrite_rules_array', 'ns_add_rewrite_rules', 10, 1);
/*
* Adds the 'media' directory to the URL of an attachment.
*/
function ns_normalize_attachment_url($url, $post_ID){
$dirname = 'media';
$post = get_post($post_ID);
return $post ? sprintf('%s/%s/%s/', home_url(), $dirname, $post->post_name) : $url;
}
add_filter('attachment_link', 'ns_normalize_attachment_url', 10, 2);
/*
* Adds the '-dl' suffix to the name of an attachment.
*/
function ns_normalize_attachment_name($data, $postarr){
$suffix = "-" . get_the_ID();
$has_suffix = substr($data['post_name'], -strlen($suffix)) === $suffix;
$add_suffix = isset($postarr['ID']) && !$has_suffix;
if($add_suffix){
$desired_name = $data['post_name'] . $suffix;
$data['post_name'] = wp_unique_post_slug($desired_name, $postarr['ID'], $data['post_status'], $data['post_type'], $data['post_parent']);
}
return $data;
}
add_filter('wp_insert_attachment_data', 'ns_normalize_attachment_name', 10, 2);
<?php
/**
* The template for displaying all attachments
*
* @link https://developer.wordpress.org/themes/basics/template-hierarchy/#single-post
*
* @package Gutener
*/
get_header();
?>
<div id="content" class="site-content">
<div class="container">
<section class="wrap-detail-page">
<div class="row">
<div id="primary" class="content-area col-lg-8">
<main id="main" class="site-main">
<h1 class="page-title entry-title"><?php single_post_title("Media: "); ?></h1>
<?php
while (have_posts()) : the_post();
get_template_part('template-parts/content', 'attachment');
endwhile; // End of the loop.
?>
</main><!-- #main -->
</div><!-- #primary -->
<?php
if (is_active_sidebar('right-sidebar')) { ?>
<div id="secondary" class="sidebar right-sidebar col-lg-4">
<?php dynamic_sidebar('right-sidebar'); ?>
</div>
<?php }
?>
</div>
</section>
</div><!-- #container -->
</div><!-- #content -->
<?php
get_footer();
C:\xampp82\htdocs\wp3\wp-content\themes\gutener-child\template-parts\content-attachment.php
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="entry-container">
<div class="entry-content">
<?php
$idi = get_the_ID();
$attachmenti = wp_get_attachment_image_src($idi, 'full');
$attachmentm = wp_get_attachment_metadata($idi);
$attachment = !empty($attachmentm) ? $attachmentm['image_meta']['caption'] || $attachmentm['image_meta']['credit'] : "";
if (!empty($attachmenti)) echo '<img class="attachment__image" src="' . $attachmenti[0] . '" alt="' . $attachment . '">';
the_content();
?>
</div><!-- .entry-content -->
<div class="entry-meta footer-meta">
<?php gutener_entry_footer(); ?>
</div><!-- .entry-meta -->
</div><!-- .entry-container -->
</article><!-- #post-<?php the_ID(); ?> -->