More Than Two Teasers Per Row In Thesis

by Russell on September 18, 2010 · 29 comments

in how tos,thesis

Having produced an earlier solution to have three teasers per row, I have had a few requests for solutions for 4 and even 5 teasers per row. These small teasers are useful where just a title and a thumbnail is to displayed but no text.

The approach I have adopted to keep the code tidy is to put the JavaScript in separate files called jquery.n_teasers_per_row.js and put this file in the custom folder where the n is either three, four, five or six. This approach keeps the PHP in custom_functions.php simple.

Three Teasers Per Row

I put the JavaScript in a file ../themes/thesis/custom/jquery.three_teasers_per_row.js

function three_teasers_per_row() {
  last_teaser = jQuery('.teaser').size()-1;
  jQuery('.teaser').each(function(index) {
     if ((index % 6) == 2) {
         jQuery(this).clone().appendTo(jQuery(this).parent().prev());
         jQuery(this).remove();
     }
     if (((index % 6) == 3) && (index < last_teaser)) {
         jQuery(this).clone().prependTo(jQuery(this).parent().next());
         jQuery(this).remove();
     }
  });
  jQuery('.teasers_box').each(function(index) {
     if (((index % 3) == 1) && (jQuery(this).children().size() == 0))  {
         jQuery(this).remove();
     }
  });
  jQuery('.teaser').each(function(index){
    jQuery(this).addClass('teaser3').removeClass("teaser_right");
  });
}
jQuery(document).ready( function () { three_teasers_per_row(); });

Here is the CSS. You may need to tweak the width according to the size of your content area

.custom .teaser3 { width: 210px; padding-right:20px; } //change width to allow 3 columns
.custom .teaser3 .format_teaser p { display: none } //hide the text but not the teaser thumbnail

Here is the PHP to gi custom_functions.php. In this example I am enabling, three teasers per row on just the home page

function add_jquery_stuff () {
   if (is_home()) {
      echo ('<script type="text/javascript" src="'.THESIS_CUSTOM_FOLDER.'/jquery.three_teasers_per_row.js"></script>');
   }
}
add_action('wp_head', 'add_jquery_stuff');

Four Teasers Per Row

I put the JavaScript in a file ../themes/thesis/custom/jquery.four_teasers_per_row.js

function four_teasers_per_row() {
  jQuery('.teaser').each(function(index) {
     if ((index % 4) > 1) {
         jQuery(this).clone().appendTo(jQuery(this).parent().prev());
     }
  });
  jQuery('.teasers_box').each(function(index) {
     if ((index % 2) == 1) {
         jQuery(this).empty().remove();
     }
  });
  jQuery('.teaser').each(function(index){
    jQuery(this).addClass('teaser4').removeClass("teaser_right");
  });
}
jQuery(document).ready( function () { four_teasers_per_row(); });

Here is the CSS that is similar to that above. You may need to tweak the width according to the size of your content area

.custom .teaser4 { width: 150px; padding-right:20px; }
.custom .teaser4 .format_teaser p { display: none }

Five Teasers Per Row

I put the JavaScript in a file ../themes/thesis/custom/jquery.five_teasers_per_row.js

function five_teasers_per_row() {
  last_teaser = jQuery('.teaser').size()-1;
  jQuery('.teaser').each(function(index) {
     i = (index % 10);
     if ((i== 2) || (i ==3) || (i==4) || (i==8) || (i==9))  {
         jQuery(this).clone().appendTo(jQuery(this).parent().prev());
         jQuery(this).remove();
     }
     if ((i==5) && (index < last_teaser)) {
         jQuery(this).clone().prependTo(jQuery(this).parent().next());
         jQuery(this).remove();
     }
  });
  jQuery('.teasers_box').each(function(index) {
     i = (index % 5);
     if (((i == 1) || (i==2) || (i==4)) && (jQuery(this).children().size() == 0))  {
         jQuery(this).remove();
     }
  });
  jQuery('.teaser').each(function(index){
    jQuery(this).addClass('teaser5').removeClass("teaser_right");
  });
}
jQuery(document).ready( function () { five_teasers_per_row(); });

Here is the CSS that is similar to that above. You may need to tweak the width according to the size of your content area

.custom .teaser5 { width: 120px; padding-right:15px; }
.custom .teaser5 .format_teaser p { display: none }

Six Teasers Per Row

I put the JavaScript in a file ../themes/thesis/custom/jquery.six_teasers_per_row.js

function six_teasers_per_row() {
  jQuery('.teaser').each(function(index) {
     if ((index % 6) > 1) {
         jQuery(this).clone().appendTo(jQuery(this).parent().prev());
     }
  });
  jQuery('.teasers_box').each(function(index) {
     if ((index % 3) > 0) {
         jQuery(this).empty().remove();
     }
  });
  jQuery('.teaser').each(function(index){
    jQuery(this).addClass('teaser6').removeClass("teaser_right");
  });
}
jQuery(document).ready( function () { six_teasers_per_row(); });

Here is the CSS that is similar to that above. You may need to tweak the width according to the size of your content area

.custom .teaser6 { width: 100px; padding-right:10px; }
.custom .teaser6 .format_teaser p { display: none }

Please feel free to make a donation is you find any of the above useful or you would like me to do for the tutorial for seven or more teasers per row.

Thanks

Russell

Google Buzz
Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

{ 29 comments… read them below or add one }

serge lerner September 29, 2010 at 4:10 am

first of all thanks alot !
the thing is i cant manage to work this… i think i do everything right. i can see that i’ve add the JavaScript file to the wp_head and it look right. the css i add though looks overwritten by something.

im using thesis 1.8, any chance this is not working on thesis 1.8 ?

do i suppose to do anything else on the back end ?

unfortunately i don’t have an online site to show you. im running this on wamp.

Russell September 29, 2010 at 11:56 am

Try using Internet Explorer and check the status bar at the the bottom left on the page for errors. Double click to see an error message. It might be that jQuery is not loaded at all or it might be that two versions of jQuery are running. These are the most likely issues.

If that does not work you can send me the html source of the page

Regards

Russell

serge lerner September 29, 2010 at 1:25 pm

I’ve checked with Internet Explorer and it does have an error message:
Webpage error details:

Message: Object expected
Line: 24
Char: 1
Code: 0
URI: http://localhost/wordpress/wp-content/themes/thesis_17/custom/jquery.three_teasers_per_row.js

what can i do ?
thanks again!

Russell September 29, 2010 at 2:04 pm

Line 24 is jQuery(document).ready( function () { three_teasers_per_row(); }); so it is probably a jQuery issue.

You need to view the page source

Is jQuery being loaded before this script?

The jquery load might be something like

<script type=’text/javascript’ src=’http://www.wordpresswise.com/wp-includes/js/jquery/jquery.js?ver=1.4.2′></script>

serge lerner September 29, 2010 at 2:17 pm

no its not being loaded…
how do i load it ?

Russell September 29, 2010 at 2:27 pm

You can safely move the three_teasers_per_row.js file into the footer (before the </body> tag where there is less chance of the problem being the load order of the scripts

serge lerner September 29, 2010 at 2:35 pm

ok i’ve managed by putting some jQuery loading code into the thesis Additional Scripts panel.
Thanks for everything! this is very helpful.

Russell September 29, 2010 at 3:03 pm

Glad you got it working!

Matt October 18, 2010 at 7:20 pm

I’m having the same problem in thesis 1.8 I went over the tutorial a few times and can’t seem to find the problem of it not working correctly. What code exactly did you insert in the additional scripts panel?

serge October 18, 2010 at 9:24 pm

Try going to the Site Options – > Additional Scripts and paste the next code:

This line loads jQuery from google

Russell October 19, 2010 at 4:31 pm

Hi Serge

Thanks for your comment. Pasting code in comments can be problematic and sometimes they get stripped out automatically by Wordpress.

I have installed the Syntax Highlighter Evolved plugin, which allows code to be displayed within posts, and have just checked that it works in comments..

Please put your javascript code between [ code][/code] tags with square brackets but please omit the extra space which I need to included here to made the tags visible (otherwise they would hav ebeen translated by the plugin

For example, this what the code looks like when surrounded by the code and /code tags

[code]<script text="javascript">javascript code here></script>[/code]

serge October 19, 2010 at 5:51 pm

Hi Russell, thanks for the update,

here’s the code that loads jQuery from google:

[code]<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>[/code]

Gregory October 26, 2010 at 9:12 pm

Hello!,
This dpesnt work with Thesis 1.8 .
Anyone able to get iot working on 1.8 ?
let me know pls.

Gregory

Gregory October 26, 2010 at 9:24 pm

Hi! Russel,
I just re checked and in IE I am getting an error. same as you mentioned above about jquery.
But when i look up the source I can see it at the bottom

This is before the tag

Does this need to be showing up in the header position or something else ?
If so how do I move the location where the javascript call shows.

Gregory

Russell October 26, 2010 at 9:52 pm

Hi Gregory,

This site is Thesis 1.8 and the 3 teasers per row is working fine on the home page. It renders fine on Firefox, Chrome, Internet Explorer and Safari on PCs and Firefox, Chrome and Safari or Macs

The issue is likely to a plugin conflict, theme issue or a javascript issue that is specific to your site.

Regards

Russell

Russell October 26, 2010 at 9:55 pm

Hi Gregory,

When clicking on the error you should get a more detailed message with a line number. Do a “view source” to discover the line.

Is jQuery being loaded at all? Are you using the Wordpress or the Google version of jQuery. Are you using Thesis’s option to include jQuery?

If you give me your URL I can take a look if you would like.

Regards

Russell

Gregory October 27, 2010 at 3:04 am

Hi! Russel,
The link to the site is http://incomeink.com/
In IE its still showing the error icon, but I am able to see the following line in the view source before the tag:

Can you pls take a look at it and suggest how I can have this working.
Thanks for your help and time.

Gregory
Gregory´s last blog ..Test Post SixMy ComLuv Profile

Gregory October 27, 2010 at 3:07 am

Sorry just noticed I had not replied to this part of your questions:
I have only enabled the Jquery from within the Thesis Javascripts in Design Options.

Gregory
Gregory´s last blog ..Test Post SixMy ComLuv Profile

Gregory October 27, 2010 at 1:51 pm

Hi! Russel,
I do not have any plugins installed except the Thesis Open Hooks. The Theme is the Free Network News from WPBlogger.com.
Look forward to your reply

Regards
Gregory

Gregory October 27, 2010 at 2:15 pm

Hi! Russel,
Ok tried the whole thing again and this time it worked.
Thanks for the help and great code.

Regards
Gregory

Russell October 27, 2010 at 3:27 pm

Glad it is all working for you

Cheers

Russell

Gregory October 27, 2010 at 9:26 pm

Hi! Russell,
Double Cheers for you – its your code that did it.

How can one remove the text from showing up and have only the images displayed.
As per your code I have the following in the custom css file but the text is still showing .
[code].custom .teaser3 .format_teaser p { display: none }[/code]

Could you tell me how to have this working without the text and showing only the images.

Regards
Gregory

Russell October 27, 2010 at 10:09 pm

Hi Gregory

I can’t see why it is not working – my suggestion is to try changing the CSS

[code].custom .teaser.teaser3 .format_teaser { display: none; } [/code]

or if that does not work, try

[code].custom .teaser3 .format_teaser { display: none !important; } [/code]

Gregory October 27, 2010 at 11:08 pm

Hi! Russell,
I tried both the options you gave… Unfortunately none of them worked.
Is’nt that really odd. I am all at sea here. :-(

Regards
Gregory

Russell October 27, 2010 at 11:29 pm

What you are doing should work.. I do not see why the CSS for .teaser3 .format_teaser is not being applied.

Your site is behaving as if the line that includes the CSS for .teaser3 display: none does not exist.

I can only suggest doing something like moving the line for teaser3 to the top of the custom.css file and retyping it carefully.

Sounds a bit crazy but I have encountered issues with non-printable invisible control characters in files that means instructions are ignored.
Why crazy? Because when I suggest you delete the invisible characters then you say “which characters?” and I say “the invisible ones” – right next to the visible ones. Sadly, this has fixed the problem more times than I would care to remember. That’s computers for you.

However, despite the rant, the problem here is completely soluble; it just takes a systematic approach to identify what is not working and why.

It is just a matter of getting the right CSS that works for your theme and your set of plugins

Gregory October 28, 2010 at 12:05 am

Hi! Russell,
Thanks for the feedback. Am going to read it again and then try it over as suggested. Hopefully it should do the trick. Will let you know how it goes.
Thanks for your tireless support on this.

Regards
Gregory!
Gregory´s last blog ..Test Post SixMy ComLuv Profile

Wo November 13, 2010 at 2:09 pm

Love this. Worked first time. It looks great. Thanks so much. One thing. I would like to see the text under the Teaser images. I took out the second line of code in the CSS but the text hasn’t come back. Any idea?
Wo´s last blog ..Christmas MenuMy ComLuv Profile

Wo November 13, 2010 at 2:11 pm

My bad. Needed to change it in Design Options. Thanks again.
Wo´s last blog ..Christmas MenuMy ComLuv Profile

Clarence Johnson November 17, 2010 at 12:58 am

Hi Russell,

Thank you for sharing this JavaScript solution.

I am having a challenge getting the “Three Teasers Per Row” to work and I was wondering if there if a step that is missing that it is presumed that the visitors like myself would know. Bear with me….

Do I need to select a JavaScript library in the Design Options—>Home Page Display Options and/or Design Options—>JavaScript?

Do I need to amend .THESIS_CUSTOM_FOLDER. portion in the PHP code that is inserted into the custom_functions or just use the whole code as is?

Thanking you in advance.
Clarence Johnson´s last blog ..Chilly EyewearMy ComLuv Profile

Leave a Comment

CommentLuv Enabled

Spam Protection by WP-SpamFree

Previous post: