Support » Fixing WordPress » Matching sidebar length to post length

  • Resolved Michael4fm


    Can anyone tell me how to (automatically) match the length of the sidebar to that of the post, and I don’t just mean making them the same size with no content.

    To explain:- I have sites where posts will be of varying lengths. I want to have a sidebar that will effectively show however many widgets needed to ‘balance’ with the length of the post – so there is no blank sidebar next to a long post, or no blank post next to a long sidebar.

    Hmmm. My difficulty in explaining it may be the cause of my difficulty in finding the solution! 🙂

Viewing 6 replies - 1 through 6 (of 6 total)
  • I understand what you want to get done. It might be a complex task.
    You can post your job at

    If the content of your posts is text which is similarly formatted in all posts you could use php function strlen to get the length of the content and use that to estimate how many widgets you need. Example

    if ( strlen(the_content()) < 200 ) {
    // One widget
    } elseif ( strlen(the_content()) < 400 ){
    // Two widgets
    } else {
    // All widgets

    Thanks for the feedback both 🙂

    @jgarcia: Oh … 🙁 thought it would be relatively easy …ish, once I knew the right direction. Probably not worth me employing someone just because I’m so anal!

    @dotnordic: That sounds like a direction worth exploring. Maybe won’t be ‘precise’, but at least it could elimininate acres and acres of white space. Thanks for the idea 🙂

    Hello Michael4fm

    Did you find a solution for that. I would be very interested…


    Hi Rangi

    Kind of … you can use the string length formula dotnordic gave above within a template.

    I ended up using several, so ….

    if( string length > 1 ) print a
    if( string length > 2 ) print a
    if( string length > 3 ) print a
    if( string length > 4 ) print a … etc. (could be print a; print b; print c; etc.)

    I used this on single post templates. A bit long winded to set the template up but once done you can forget about it. (In my case, I also made an extra custom field to generate 1 | 2 | 3 extra ‘a’s, if they were needed for a particular post – for example because there were lots of sub-headings and short sentences which meant the actual string length of the content was quite low. I don’t use this any more, so can’t point you to an example, sorry.

    If you want to match sidebar length on pages with multiple posts – e.g. category lists, post lists, etc., I’ve cheated. Instead of having a sidebar, I include what would have been in the sidebar within the post format. This only works where you want the same thing, or sort of thing, for each listing. I use the RANDOM TEXT plugin so that the wording in the template is constant, but the actual output is random.

    Example here: A category page on my site

    (Some of) the items on the right are randomly generated by that plugin. If I changed the settings to list eight posts instead of four, it would generate eight (or however many are needed) ‘extra’ items, so there’s no ‘white space’. This example actually generates the content randomly twice – which category it prints from is chosen randomly from a list of possible categories I’ve defined. Clever, in a way, I thought … but actually really easy to do, once you understand the basic idea.

    If you want to show ‘widget 1’ for short lists, if it’s long enough show ‘widget 2’, if it’s still longer show ‘widget 3’ etc. … you could do that via the top method. No reason it wouldn’t work on a multiple posts template as well as a single one. Just a bit harder – I guess – to automatically work out how long the string is, and you’d have to decide in advance a maximum length, so you’d know how many “if” statements you need.

    Hmmm. I may have made it sound more confusing than it actually is! It’s fairly easy. Just takes a little time and experimenting to get it to work / look how you want.

    Hope this helps. Feel free to ask a more detailed question if you decide to try one of the methods and get stuck. I’m not ‘all knowing’ but at least I have the benefit of some small amount of experience.

    Best regards

    Hi again Rangi

    I’ve actually found a file copy of my template for single posts with this in. Sometimes it’s easier to start from something and adapt it to do what you want, rather than start from scratch. At least, I find it so. If a copy of it would be helpful, just ask! 🙂

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Matching sidebar length to post length’ is closed to new replies.