how to create a conditional content_width for a wordpress theme?

I’m trying to create a conditional statement for the content width in the functions.php of my theme. So some categories will have a content_width of 580, some will have 900. This will make the oEmbed work correctly wherever it’s used.

Usually, you would have this in your functions.php:

Read More
if ( ! isset( $content_width ) ) 
    $content_width = 900;

I want something like this:

if(in_category(array('news','blog'))) {
   $content_width = 580;
} 
elseif(is_page()) {
   $content_width = 580;
}
else {
   $content_width = 900;
}

When I have this in my functions.php the content width becomes whatever I write in the last line.

Am I missing something? Is this even possible?

Thanks!

Related posts

Leave a Reply

2 comments

  1. No, its not possible. $content_width is a theme-wide constant, and its set in functions.php before any of the query conditionals are set.

    $content_width is used to determine the intermediate image sizes in image_send_to_editor. The “large” image size will be set to the value of $content_width.

    If you need to modify those sizes on a per-category basis, you can hook into that filter… see my answer on the question change set_post_thumbnail_size according to post type admin page for a general example of how this can be done (note that this will almost always fail if you’re trying to do it by category, as the post category is almost always not set when the user first uploads an image – setting it by post type is a little more reliable)

    No matter how wide your posts and pages are, set $content_width to the widest image you want a user to include in a post.

  2. Maybe this works:

    if(in_category(array('news','blog'))) {
       $GLOBALS['content_width'] = 580;
    } 
    elseif(is_page()) {
       $GLOBALS['content_width'] = 580;
    }
    else {
       $GLOBALS['content_width'] = 900;
    }