Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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:

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!

share|improve this question
    
why not just set different widths in the css put php on the body tag to display which category it is and then add different css to each tag? –  user10557 Nov 24 '11 at 17:04
add comment

2 Answers 2

up vote 7 down vote accepted

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.

share|improve this answer
    
Fantastic! Thanks for your answer. –  SteveLambert Jan 7 '11 at 5:08
add comment

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;
}
share|improve this answer
3  
Please add an explanation to your answer: why could that solve the problem? –  toscho Aug 9 '13 at 20:04
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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