7

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!

1
  • 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, 2011 at 17:04

2 Answers 2

9

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.

0
0

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;
}
1
  • 5
    Please add an explanation to your answer: why could that solve the problem?
    – fuxia
    Aug 9, 2013 at 20:04

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.