Snippets: Change the Attachment Page Layout

The following snippet can be used to alter your attachment.php page layout. This is the page that is used to display attachments (images, videos, pdfs, etc) on the front-end. By default the attachment page uses the same layout as your standard pages which you can alter via the Customizer.

/**
 * Filters the post layout class.
 *
 * @param string $class (options full-screen, full-width, left-sidebar, right-sidebar)
 */
add_filter( 'wpex_post_layout_class', function( $class ) {
    if ( is_attachment() ) {
        $class = 'right-sidebar';
    }
    return $class;
}, 20 );

Snippet Type: PHP

Posted Under: Other

All PHP snippets should be added via a child theme's functions.php file or via the Code Snippets Plugin (or alternative plugin)