In 2016 theme, how can I force all single attachment posts to use content-attachment.php rather than image.php?

There’s a couple things you could do. If we follow the Template Hierarchy you could rename your content-attachment.php file to attachment.php and remove image.php. This will ensure that the image view will fallback to attachment.php.


If you want to use content-attachment.php then you could use the template_include hook to tell WordPress to load in which situation.

/**
 * Tell WordPress to load a specific template under specific circumstances.
 *
 * @param String $template_path
 *
 * @return String $template_path
 */
add_filter( 'template_include', function( $template_path ) {

    if( false !== strpos( $template_path, 'image.php' ) ) {

        // Path to your PHP file, relative to the twentysixteen theme (or child theme)
        // This would mean `content-attachmetn.php` is in the twentysixteen root, with page.php and singe.php
        $template_path = get_template_part( 'content-attachment' );
    }

    return $template_path;

} );

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)