I’ve published a Premium Genesis child theme called Plastic Pro with a masonry layout with infinite scroll. I thought it might be a good idea to write a quick tutorial about it. So here we go!
I’ve tested this with my own Genesis basic theme and with the Genesis Framework sample theme. In both cases it worked fine so there is no reason this shouldn’t work with other Genesis themes.
With the inclusion of jQuery masonry in the WordPress 3.5 core library, it’s very easy to change a template’s layout into a masonry layout. But what is exactly a masonry layout?
A masonry layout resembles a brick wall. The posts or images are the bricks in the wall. Curious about an example? Here you find a screenshot of the Plastic theme I’m working on.
Step 1 Create a page template
Before we can start building our masonry we need to create a new page template and load the appropriate scripts. Add the following code to the new file and save it as masonry-scroll.php. If you’re lazy you can grap the whole template at once, just scroll down.
Step 2 Download and set the scripts
To get everything to work we need to download the following script and place this in your child theme directory in a subdirectory with the name js.
In the subdirectory js we subsequently create two new files. The first is init-masonry.js. Add the following code to this file.
The second file is masonry-isotope.js Add the following code to this file.
Step 3 Add custom class to posts
Add the following code to the file we’ve created in step 1. With this snippet we add a new class (.brick) to the posts on this page.
Step 4 Create the loop
Additionally, add the following code to the file we’ve created in step 1.
In this example I’ve set the post per page at 6. Change this to your liking.
Step 5 Add infinite scroll
Now, it’s time to add some magic. Add the following code beneath the loop.
Step 6 Build the page
Finally, Genesis need to build the page. Add the following code at the bottom of the page template we’ve created.
Final Step Create some CSS and set the page
Without any CSS the masonry layout isn’t very pretty yet. Let’s add some basic CSS to create the bricks and hide the navigation at the bottom of the page. Finally, to make it work, create a new page in WordPress and set the page template to Masonry Grid Scroll.
Download full page template here
Final Words
I hope this article is useful for Genesis developers or users wanting to use masonry and infinite scroll with Genesis. When I was writing this tutorial it looked quite simple. However, it did take me some time to figure it all out. This tutorial hopefully saved you some time. Any suggestions or (positive) feedback? Leave a comment and follow me on Twitter for more tutorials in the near future.
Brad Dalton says
Hello frank
I saw you tweeting about this several weeks ago so i assume you have put in alot of time and effort to work out this solution.
I’m working on something similar and also filtering the post class but might build a new loop rather than use the custom loop. Don’t think i’ll add the infinite scroll though.
Demo looks great. https://wpstud.io/demo/plastic/
Good luck with sales of your theme!
frank says
Hi Brad,
Thanks! It took some time to get everything to work. The documentation of both scripts helped me a lot. After i’ve got everything into the theme, the tutorial was quickly written.
I’m curious about your solution.
Frank
Brad Dalton says
Hi Frank
I published a post about it today. Works perfectly when using Chrome but slight problem in Safari. Didn’t add CSS for Media Queries but looks like a bit of work to get the responsiveness perfected. I didn’t use columnWidth: either. Didn’t use a custom loop or filter the post class. Just used CSS for the width. I think it will need images loaded if i add featured images so still some work needed for that. Looking forward to seeing your theme when its finished.
satrap says
Thanks for this Frank.
Is there anyway to display the posts in a brick style–Have one post display full width, then two posts under it each covering 50% of the width, and repeating this set up over and over?
Like so:
first post full width
2nd post 3rd post
4th post full width
5th post 6th posts]
and so on…
I assume it has a lot to do with the css, but I am not that advanced. Thanks.
frank says
Hi Satrap,
I think that can be done with some extra CSS. You can use the CSS pseudo-selector nth-child to achieve the result you want. With this pseudo-selector you can for example every second and third brick.
For more information have a look at: https://css-tricks.com/how-nth-child-works/
Frank
Satrap says
Thanks very much Frank.
Yeah, that’s a little over my head. And since I need this quickly, I am going to have to hire a developer to do it.
Meanwhile, thanks for the link. It will be a great resource for me to start learning about it little by little. Thanks.
Dustin says
Just curious. If you did not want to use infinite scroll and just wanted to use pagination how would you accomplish that? Thanks for the article!
Frank Schrijvers says
Dequeue the infinite scroll script. And change the css so the page navigation is visible. Without testing I think that’s all.
Roberto says
Great work! it was very helpful. I’m interested in use a paginator too, and I commented the infinite scroll script and I changed the css, but if I click “next page” it send me to the correct page “../page/2/” but always show me the content of the first one.
I tried commented too the line:
$paged = get_query_var( ‘paged’ ) ? get_query_var( ‘paged’ ) : 1;
and
‘paged’ => $paged,
But it’s still don’t working. I’m going to keep trying, but maybe you could have an idea of what happen. Once again, thank you for the post.
Frank Schrijvers says
Hi Roberto, I’ve tried this is a local environment and it work when I disable the ‘custom_infinite_scroll_js’ function. You have to leave te $paged variable as it is. So I’m not sure why it does not work for you.
Roberto says
Thank you for your answer. I just figured out that my problem only happens when I select the masonry page as “front-page” in the wordpress settings, so maybe could be a configuration problem of my site.
Roberto says
Hi, I found a solution. This is a general problem of wordpress, it happened me even with other blog-pages and I’m not very sure why, but it’s related to the use of ‘paged’ and ‘page’ in the loop.
By accident I changed the script to this:
$paged = get_query_var( ‘page’ ) ? get_query_var( ‘page’ ) : 1;
And it’s works 🙂
So, I search an explanation of what I do and I found this (in the static front page section:
https://codex.wordpress.org/Pagination
Note: this is only necessary if the blog-page is used as static page, if not with ‘pageg’ works fine.
Dustin W. Stout says
Thanks so much for this tutorial! I’ve been wanting to try this out for a while but hadn’t found a good walkthrough until now. However, despite following everything perfectly it doesn’t seem to want to work on the Altitude child theme. Worked perfectly on the Genesis Sample child theme, so I’m guessing it’s some sort of Javascript conflict. Not sure yet– Javascript and PHP aren’t my wheelhouse.
subversiv says
Hi, Great Tut. How hard would it be to put an overlay over the image and to put the title below. Thanks
Frank Schrijvers says
If you’re familiar with the Genesis hooks and CSS this is not that hard. I guess you have to remove the content of the posts on the blog page with the Genesis Hooks (remove_action).
For the overlay add some html (div or span with a class) above the featured image (this can be done with the add_action). This div or span you can style as a overlay.
kaushik says
Well for naive users, i just created a plugin which has this both functions !
The plugin will be soon published on WordPress.org…
You can see demo at
http://www.kaushiksomaiya.com/demo/
kaushik says
Now Available at https://wordpress.org/plugins/gmis/
Kneops says
This plugin looks great. The loading is somewhat quirky though and some optional settings would be great.
Dianne says
Hi Frank,
I really admire this tutorial it really helps me alot however I couldn’t get the height of the #container to work when I load the page. I used the manual trigger but when I load the page, the height doesn’t adjust. can you help me?
Frank Schrijvers says
The height is set by the script. So my best guess is that you have already set the height of the #container in your style.css.
Julia says
hello! thank you for this.
I do have a question though.. how do i go about creating an infinite scrolling masonry on an inner page, not the front page.. is it somewhat the same as what you have detailed here?
thanks!
Julia says
oh nevermind! sorry i missed the part where you said i could use it as a template on any page. way cool i will try this now! 😀
Micki says
Will this work for woocommerce using Genesis connect for woocommerce plugin? Thanks
Frank Schrijvers says
This will only work with posts. If you want to load single products you have to change the loop. More info about a product loop can be found here: https://docs.woothemes.com/document/sample-products-loop/
Brandon says
This is a great tutorial, and I’ve been working on something to have some bricks 2 column, and maybe even 3 column. The problem is the bricks all then show left: 0 and stack rather than fill in the space. Any thoughts on a fix?
Frank Schrijvers says
Hi Brandon,
Not really, it could be a CSS or a Javascript issue. Did you enqueued every script? And can you access the script in the source code? If every thing is set, I guess it’s a CSS issue.
Brandon says
I think it might be best to set up a “staging/dev” site so I can show you. I went a lot off your tutorial, and I believe everything is queued. When I open Dev Console I’m not seeing a JavaScript error thrown. Once I get the staging/dev site set so I can give an example, would you be willing to take a look and offering up a suggestion?
Frank Schrijvers says
Hi Brandon,
I can always give it a quick look. Just give me a sign when the staging site is online.
Doris says
Hi, I’m trying to follow along with this and think I have a general idea of how to make it work, so please bear with me since I’m new to developing, but where does the “masonry-scroll.php” file need to go? Do I just upload it into the theme’s folder without a subdirectory? I appreciate any input! Thank you!!!
Doris says
Also, another question: does each post need to have “brick” in the Custom Post Class section?
Frank Schrijvers says
Every post needs the class brick to make things work, so the answer is yes 🙂
Doris says
Again, bravo for the quick response! Thank you so much! This tutorial is top notch! 😀 Cheers!
Frank Schrijvers says
Hi Doris,
The masonry-scroll.php file can be placed directly in the child theme directory.
Doris says
Awesome, you’re the best! I really appreciate your prompt response! 🙂
Candiss says
Awesome tutorial. I have the Plastic theme and love it.
How would you add a filter section at the top of the grid. If someone wants to see just things in the photography category or illustration category, how can I add this feature? Thanks.
Frank Schrijvers says
Hi Candiss,
Infinite scroll and filtering is a complex combination. A good starting point is http://isotope.metafizzy.co. An explanation on how to include this functionality into a child theme falls outside the scope of this tutorial.
Candiss says
Great link. I also discovered wpclips. I will try this tomorrow and see how it goes. Thanks for the great tutorial.
Thomas Speak says
First of all, thank you so much for the tutorial. The Genesis community it amazing!
I have this working.. Kind of here.
http://www.myresidentlinc.com/blog/
But the masonry is not working it seems. If this is easy to spot the issue I need some help please.
Frank Schrijvers says
Hey Thomas,
If I look in the console I see you’ve got some 404 error’s on your site. It fails to load the jQuery files, so I think something is wrong with the path or you didn’t include the masonry-isotope, init-masonry and infinitescroll files?
Frank