Searching here I've found two questions related and at the time, this wasn't possible.

how to create a conditional content_width for a wordpress theme?

And an alternative way via CSS described by Philip Arthur Moore (the code is also on Codex): What is the role and history of the $content_width global variable?

Since the questions mentioned are a little old, my question is if the versions released after mid 2013 have improved the function so we could use it with conditionals.

1 Answer 1


As of now there is no direct way to conditionally set content_width.But we can do this using template_redirect hook and changing the global content_width.This is done in Twenty Fourteen Theme.Following is the code.

 * Adjust content_width value for image attachment template.
 * @since Twenty Fourteen 1.0
function twentyfourteen_content_width() {
    if ( is_attachment() && wp_attachment_is_image() ) {
        $GLOBALS['content_width'] = 810;
add_action( 'template_redirect', 'twentyfourteen_content_width' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.