The Stiz Media, LLC

Making your website work for you

  • Styles for Genesis
    • FAQs
  • Cart
  • My Account
  • Home
  • Web Design
  • Portfolio
  • Resources
  • Blog
  • Contact

Powerful websites, with style.

Home » Blog » Tabbed Content via AJAX in Genesis

Tabbed Content via AJAX in Genesis

January 20, 2015 Andhi 3 Comments

Tabbed Content via AJAX

This tutorial adds loading tabs with Ajax to my last tutorial about creating a tabbed content page template. Watch a quick video below to see it in action.

I’m going to assume you successfully followed my last tutorial, and you have a working page template with your tabbed content. To load your content via ajax there are a few small, but extremely important steps to get it all working.

Typically, your first tab will be open by default, so you want that content to load on page load. That being the case, your first tab will stay the same.

If your tabs will only load static HTML content like text and images, it’s very simple… instead of linking your tabs to a div ID on the page, you link to new .html or .php file with your content. You don’t even put the content div on the page, tabs-ui created it for you.

My tab links could look something like this:

Naturally, you’ll have to have the existing files in the proper location. My example shows has them in:
/wp-content/themes/child-theme/includes/tabs/filename.html, but you can put them wherever you’d like.

Out of the box, this will work to load the post with ajax. We’re going to take this a step further though.

Ajax loading – a step further

Let’s say your tabs contain a lot of images. The default tab functionality will require re-loading those images every time the tab is clicked, even if you don’t refresh the page. That’s not cool at all. We’re going to temporarily cache the content after each tab is loaded. This requires some additional javascript to get it to work properly. This tutorial loads our javascript in a file called tsm-tabs-ajax.js.

You can see by the snippet notes what each additional bit of code does. Before loading the content we check if the tab has already been loaded, and if it has we show the cached data. If it has been loaded, we load it and set the data as ‘loaded’ so it can be cached.

Lastly we run a loading notification.. I used Font Awesome for an clean spinning icon but you can just put simple text or whatever you want here.

As a fallback, if something goes wrong we show an error message so the user doesn’t just sit there wondering if content is supposed to load when it’s failed.

Loading posts or other WordPress/Genesis loops

UPDATE: Since writing this post, I’ve now learned (and understand) why this next part is pretty horrible and you should never do it this way. It works, but you probably shouldn’t do it. I’ll leave it here so you can learn, but again… don’t do it!

Things get really tricky when you try to load posts or use other WordPress functions when loading content via jquery tab ui. The problem is that loading the files is actually happening outside of WordPress. There is a little trick that will run just enough of WordPress to retrieve the posts/info we need. This goes at the top of one of the tab.php files you are loading your content from.

The first part tells the file NOT to load the theme. This is important because the theme files will already be loaded on the page we are calling the tab content from.

The second part loads the initial file need to get WordPress running so we can pull in our posts. This part is tricky since you need to back out of the directory your file is in enough times to get right to the wp-load.php file.

My tab file is located at:
/wp-content/themes/child-theme/includes/tabs/filename.php
We need 5 sets of double dots “..”. One set for each directory we need to get to the root of our WordPress install, which is where wp-load.php is located. You may have to change this accordingly to get to the correct location.

After this is running, we now can run WP_Query or genesis_custom_loop() to retrieve posts.

Get posts with WP_Query

This is the code in my tab_3.php file

Get posts with genesis_custom_loop()

This is the code in my tab_4.php file

I didn’t show tab_2.php or tab_5.php (from the video) since they are straightfoward. Tab 1 has the post editor content. Tab 2 just had basic HTML, you don’t need anything else there. Tab 5 just had the twitter embed widget code.

That’s all there is too it!

The Final Code

My page template is relatively simple now, since most of the fun stuff happens in external files.

Don’t forget to look at the original post to grab the CSS and to go over the initial tab setup.

Related Posts

  • Tabbed Content Page Template
    Tabbed Content Page Template In Genesis
  • A really organized life
    Genesis Page Templates in a subdirectory
  • Use WP_Query To Alter The Posts Per Page Of A Custom Taxonomy Term Archive Of A Custom Post Type

Tutorials Page Templates, Tabs

‹ Tabbed Content Page Template In Genesis
Different Templates For Parent And Child Category Archives ›

Try The Most Powerful Genesis Child Theme Ever Created

Mai Theme is the most customizable Genesis child theme (and plugin!) available. Check it out now!

Learn More @ MaiTheme.com

Our posts, directly to your inbox

Comments

  1. thomas chang says

    December 25, 2016 at 3:04 am

    Hi Mike,

    Thanks for the two tutorials on tabbed content.
    But could you elaborate why in the middle of this tutorial you said “this next part is horrible”, and “shouldn’t do it”.

    Thanks,
    Thomas

    Reply
    • JiveDig says

      January 9, 2017 at 9:53 am

      Hey Thomas, here’s a start: http://ottopress.com/2010/dont-include-wp-load-please/

      Reply
  2. Boyong says

    August 15, 2019 at 3:41 am

    Hi Mike
    Could your please give tutorial for loading tabs with Ajax INSIDE wordpress?
    Thank you

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Our posts, directly to your inbox


Howdy, I'm Mike Hemberger. I like to build nice websites and web apps with WordPress and Genesis Framework.

I'm passionate about everything I do, and it resonates in all of my work.

Feel free to email, and you'll be speaking directly with me.

Recent Posts

Restful P2P Example

Create Posts 2 Posts ‘connections’ with the WordPress Rest API

EA Share Count SMS button

Add SMS share button to EA Share Count plugin

ACF Extras

ACF Extras officially launched! A community of code snippets and field groups

Remove Items from TinyMCE editor

Remove Buttons/Items From The WordPress TinyMCE Editor

  • My Account
  • Affiliate Area
  • Log In|Log Out
  • GitHub
  • Instagram
  • Twitter
  • YouTube

Copyright © 2025 · The Stiz Media, LLC · All Rights Reserved