Thesis Tip #3 – Multiple content areas

by Bill on August 26, 2009

Post image for Thesis Tip #3 – Multiple content areas

(This tutorial is specifically for the Thesis theme, but can be adapted to work with any Wordpress theme).

Wordpress works great if you want to display a title and then a block of content, but if you’re using it as a CMS you’ll often be faced with the problem of multiple content areas. You might need multiple boxes on a homepage, or 3 columns of text on an inner page.

There’s many ways to go about this. Some of them include:

  • Including <div>’s in your post content. This will work well for a developer like me who understands code, but for clients it is not usually a good option. They will typically work in the Visual mode, and can easily delete or break the<div>’s, then not know how to add them back in HTML mode.
  • Using sidebars and widgets to display content. This is good for one-off things like sidebars (same across the site) or boxes on a homepage (only used once), but it doesn’t scale well – you don’t want 20 different sidebars for different areas of your site. It also disconnects the content from the page. It’s nice to have all the content that will appear on a page editable in the same place. (This goes back to my principle of making client’s sites easy to use and hard to break).

For a project I’m working on now we needed two or three columns of content on every page (here’s an example on the client’s site). All the blocks of content are unique to that page. This solution was heavily inspired by a post at kriesi.at, but I’ve modified it with the help of Chris Bratlien to make as many content areas as you like (the code from that post only creates a left column and a right column).

The Content

When you’re writing your content in Wordpress, simply break up the content sections using an <h4> heading. I chose <h4> because it’s easily accessible from the menu bar. Select the text you want as your heading, click on the last icon in the menu( )  then click where it says Paragraph and go down to Heading 4. You could change this to whatever heading you’d like; I chose Heading 4 because I seem to only use <h1>, <h2> and <h3>.

The Code

In your Custom Functions file, paste the following code:

function multi_content($content){
$columns = explode('<h4>', $content);
$i = 0;
foreach ($columns as $column){
if (($i % 2) == 0){
$return .= "<div class=\"column\" id=\"content-$i\">" . "\n";
if ($i > 1){
$return .= "<h4>";
}
} else{
$return .= "<div
class=\"column\" id=\"content-$i\">" . "\n <h4>";
}
$return .= $column;
$return .= '</div>';
$i++;
}
if(isset($columns[1])){
$content = wpautop($return);
}else{
$content = wpautop($content);
}
return $content;
}
add_filter('the_content', 'multi_content');

Here’s what that is doing:

  • $columns = explode('<h4>', $content); Break up the content into separate blocks every time you find an <h4>.
  • $i = 0; Make a counter (called i) and start at 0.
  • foreach ($columns as $column){ For every block of content…
  • if (($i % 2) == 0){ If i is an even number (0, 2,4…) …
  • $return .= "<div class=\"column\" id=\"content-$i\">" . "\n"; Add a div with the class of “column” and the id of “content-[i]“, so content-0, content-2…
  • if ($i > 1){$return .= "<h4>";} If this is any block of content except the first, add back the <h4> (our first block of content was everything before the first <h4>, so we don’t want to add an additional one)
  • } else{ $return .= "<div class=\"column\" id=\"content-$i\">" . "\n <h4>";} If i is an odd number (1,3,5…), add a div with the class of “column” and the id of “content-[i]” and an <h4>.
  • $return .= $column; $return .= '</div>'; $i++;} Add the block of content and close the div at the end. Then move the counter up one and start again.
  • if(isset($columns[1])){ $content = wpautop($return); If there’s only one block of content (no <h4>’s were used), ignore everything we did and just show the content.
  • }else{ $content = wpautop($content); } return $content; } If there was more than one block of content, replace the default Wordpress content with the div’s and content we just did.
  • add_filter('the_content', 'multi_content'); Run this filter every time Wordpress wants to see the content of a post or page.

You’ll then need to add the appropriate css to make it behave as you like. If you want to apply css to all the columns, use the class “column.” If you want to add css to individual columns, use their id’s. Here’s an example:


.column {float: left;}
#content-0 {width: 400px;}
#content-1 {width: 300px;}

I hope that helps some of you. I know I will be using it a lot for client websites.

All Thesis Tips:

Bill Erickson is a WordPress Consultant who builds custom websites using WordPress as a CMS and Thesis as a framework. He’s a cofounder and resident of The Creative Space, and a cofounder of the BIL Conference (the open analog to the TED Conference).

This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

{ 28 comments… read them below or add one }

Julie Gomoll August 29, 2009 at 1:03 pm

Awesome. Really eager to try this. How different is this solution than just tweaking the "Teaser" options in the Thesis Theme?

Reply

David September 11, 2009 at 9:41 pm

How would you apply style to this new div's that you are creating on-demand?

Reply

Steve September 23, 2009 at 5:33 am

Bill,
This is a brilliant idea. I have tried it on a simple test site, (not using the thesis framework) just a barebones theme and have two Queries.

This might be my initial example, it seems to an extra blank div "content-0" before the working divs of class "content-1" "content-2".

The h4 heading appears in the div for me at the top working divs of "content-1" and "content-2", I would want the seperator h4 or whatever tag to clearly identify the sections on the input page, but not to display the h4 header text in the actual content divs on the output of the page. Is this possible? New to WP please be gentle with me.

Steve

Reply

Michael October 3, 2009 at 4:04 pm

I can't get this to work on the content area. Do I need to apply a css to it?

Reply

Laura October 9, 2009 at 8:26 pm

Thanks Bill,
Beautiful solution! It is just what I was looking for.

With this, I had my pages all laid out in an hour! Yeah for Wordpress, Thesis, CSS and Bill Erickson!

Reply

michaelcummings October 10, 2009 at 12:32 am

Cool idea man… I'll try it out later.

Reply

Navin Poeran October 18, 2009 at 7:38 am

I read your post, I'm trying to build something like this http://bit.ly/AniYf not sure if its the same thing though.

Reply

Judd Dunagan November 15, 2009 at 1:12 pm

Hello,

I really like the post. The idea is simple enough and I was able to modify the functions page. I see the purpose of the H4 tag so you can use the editor, however doesn't this hurt results for search engines if you spam the h tags and I thought you are supposed to only put normal content inside h tags for best practices. So I did the div way, but thaat defeats the whole idea to make it easier for the client. I am at a lost because I have check out every option to make editble content areas on one page. A plug in that would allow you to add a div box specify the size and add css values would be the missing link for me. Any ideas?

Reply

Amit Kumar Jha December 2, 2009 at 5:34 am

I am new to the wordpress. I just pasted the above-mentioned code in the custom-functions.php file. I entered the following content into the wordpress text editor in page section. lorem ipsum dolor sit amet

<h4>Column title</h4>
lorem ipsum dolor sit amet

But it did not yield any changes. I am completely new to the wordpress. So I am quite confused.Please help me. Thanks in advance.

Reply

Bill Erickson December 2, 2009 at 7:06 pm

My guess is that it's creating the div's but you haven't provided any styling to those div's. Add this to the custom.css:

.custom .column {float: left;}
.custom #content-0 {width: 400px;}
.custom #content-1 {width: 300px;}

Reply

Ashton Brown December 7, 2009 at 12:43 am

Great idea. As a designer, multiple columns in the content area has always been my biggest challenge with WordPress. I've been working on a mulit-column css framework for the content area in Thesis. I'll have to show you when it's done.

Reply

Alex December 15, 2009 at 3:54 pm

this looks really promising. I am currently working for a client, who wants 3 columns on all of his pages, so this might be the solution to that. gonna try it out immediately! great work.

Reply

Bob Lalasz January 1, 2010 at 12:46 pm

Bill, does this work if you wanted three separate blogging columns on a single page?

Reply

Bill Erickson January 1, 2010 at 12:55 pm

If you're asking if this allows you to have a three-column blog post, then yes. If you're asking if this solution allows you to have three columns across which blog posts appear (different posts in each column), then no. For the latter, you'll need to create a custom page template.

Reply

steve January 12, 2010 at 6:23 pm

Bill,
Thanks so much for this! This is a KILLER way to break up content on a page quickly and easily. I have a question…I am making a landing page for a client and I am using your methodology and it looks great in chrome, safari and firefox..but son of a gun if it doesn't look weird in IE–am I missing something?? Here is a link to the page: http://specialoffers.stopsnoringbrez.com/

Any help you could give me would be appreciated. Take care

Reply

steve January 12, 2010 at 8:47 pm

Thanks bro. I will. Great job again. Thanks for the knowledge kick down!

Reply

Bill Erickson February 3, 2010 at 7:20 pm

Can you send me a link to a page that's supposed to be multiple columns?

Reply

Dee February 3, 2010 at 7:41 pm

Here is the link http://www.compitenthosting.com
BTW I tried with just 2 <h4> content topics thinking the code you gave would just work with 2 columns but still it only shows 1 column in the diaplay. Please take a look and let me know how to get 3 colums showing . Thanks for your help.

Reply

Dee February 3, 2010 at 8:39 pm

I tried what you recommended and its sort of works. BTW I added a fourth block using a <h4> tag since the 3 colums didnt look so good. besides a 2×2colum display would give a better balance to the page.
Here is what I have in the custom.css as recommended.

/* Added for custom content layout on front page */
.custom .column {width: 300px; float: left;}
.custom .format_text {clear:both;}

It doesnt all look well. Could you please take a look at it and guide me what to do.
Also where do I go make the changes within theseis so that the <h3> tags look similar to <h4> tags.

I was reading your other posts and came across this bit of code:

.[index_page] .column {float: left;}
#content-0 {width: 300px;}
#content-1 {width: 300px;}

which I has earlier put in the custom.css.

Shouldnt i be putting something that just gets this for the index page to look etc Since I do not want <h4> tags across the whole site to be effected.

Look forward to your reply and Thanks a Ton for helping out. Cheers!

Reply

Bill Erickson August 29, 2009 at 2:36 pm

The teaser options apply to post listings on the homepage. The actual content in the teasers is in individual posts, and aggregated on one page.

This solution is like adding a "teaser look" to single pages, except all the content is within that single page (so if you go to Pages > Edit, you can change all of it).

Reply

Bill Erickson August 29, 2009 at 2:36 pm

The teaser options apply to post listings on the homepage. The actual content in the teasers is in individual posts, and aggregated on one page.

This solution is like adding a "teaser look" to single pages, except all the content is within that single page (so if you go to Pages > Edit, you can change all of it).

Reply

Bill Erickson September 23, 2009 at 4:23 pm

The way it's designed is to use <h4>'s as breaks between divs, so all the content before the first <h4> is in the first div, then <h4> and the rest of the content is in the next div (and continues for each <h4>).

With regards to the second request (not displaying the <h4>), simply put this in your css: h4 {display:none;} That's what I'm doing for the client site I built this for, as not every column has a header.

Reply

Andrew October 9, 2009 at 10:08 pm

I am having the same issue. I am new to php and I am not sure what is the right format of the custom functions page.

Would it be possible to post the actual content of the custom functions page including any headers or <?php tags, etc?

Reply

Bill Erickson October 9, 2009 at 11:36 pm

You can just paste the function at the very bottom of custom_functions.php. If you haven't edited it before, you will paste it below this:

<?php
}

(That brace is closing the custom_bookmark_links() function ).

Reply

Andrew October 10, 2009 at 12:15 am

Works like a charm!!! Thank you very much.

Reply

Bill Erickson October 9, 2009 at 11:32 pm

If you want to style them at a sitewide level, simply use the id or class of the div's. If you want to style them at a page level, add the page's slug as a class to a containing div ("content", "wrapper"…), then use them in combination:

.[page slug] .column { [style] } provides style for all the columns on a page

.about #column1 {width: 500px; float left;} provides style for just column1

From iPhone


Bill Erickson
Wordpress Consultant
<a href="http://www.billerickson.net” target=”_blank”>http://www.billerickson.net

Reply

Bill Erickson October 19, 2009 at 3:20 pm

Yes, this will allow you to do that. The code simply wraps your content in divs, so if you're content looked like this:

lorem ipsum dolor sit amet

<h4>Column title</h4>
lorem ipsum dolor sit amet

It would change it to:

<div class=\”content\” id=\”content-0\”>
lorem ipsum dolor sit amet
</div>

<div class=\”content\” id=\”content-1\”>
<h4>Column title</h4>
lorem ipsum dolor sit amet
</div>

You could then use CSS to do whatever you want with those two divs

Reply

Bill Erickson November 15, 2009 at 1:41 pm

Well I originally designed it with the plan that each column would have header text (an h4) describing it. The client then wanted some columns to have it and other columns to not, which is why I added the display:none.

Even when you're hiding the h4, including a relevant description of the content is recommended for SEO purposes and screen readers. If I'm using display:none and have a title in the div, then I usually use \”column 2\” or \”right column\” as the h4 as it describes the content. This won't hurt SEO, as it's descriptive.

From iPhone


Bill Erickson
Wordpress Consultant
http://www.billerickson.net

Reply

Leave a Comment

Previous:

Next: