• Hi All!

    I have a wp page that shows the daily menu of a restaurant. The tabs are named Monday, Tuesday …. and I’d like to find a way to set the default/active tab by the day. I want to keep the whole week, 6 tabs on this site, but I want to have the timely appropriate to show. So when someone visits the site on Thursday, the Thursday tab will be active and not the Monday one, as it is the first and active one.

    Can anyone share info on how to do it?

    Thanks,
    vik3d

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    It depends on what code is executed to bring any particular tab to the front. There’s all sorts of ways to do this. For example, each tab could have a javascript function called from the tab’s onclick event that brings the tab’s content to the front.

    If that’s the case, have some script that runs on page load determine the current day of the week and then call the appropriate javascript function as though someone clicked on the proper tag.

    That’s one example for a specific case, what you need to do depends on how switching tabs is implemented. The basic idea is to cause script to run on page load as though someone clicked the proper tab.

    Thread Starter vik3d

    (@vik3d)

    Thanks bcworkz,
    Yes a javascript activates the tab is clicked on.

    <script type="text/javascript">jQuery("#tabwrap1 a").click(function (e) { e.preventDefault(); jQuery(this).tab("show"); });
    		if(jQuery(".nav-tabs").length > 0){
    			var url =  jQuery(location).attr("href");
    			var idx = url.indexOf("#");
    			var hash = idx != -1 ? url.substring(idx+1) : "";
    			if(hash != ""){
    				jQuery(".tab-content #"+hash).siblings(".tabs-container").removeClass("in active");
    				jQuery(".nav-tabs a[href=\"#"+hash+"\"]").parent().siblings("li").removeClass("active");
    
    				jQuery(".tab-content #"+hash).addClass("in active");
    				jQuery(".nav-tabs a[href=\"#"+hash+"\"]").parent().addClass("active");
    
    			}
    		}
    	</script>
    Moderator bcworkz

    (@bcworkz)

    Each tab’s container has a particular ID. Examine the HTML source to determine what that is. Create a script that runs on page load that determines the ID based on the day of the week. A switch/case structure can do this. Also initially remove all “active” and “in active” classes from all tab related containers in preparation for creating a new arrangement. Add an “active” class to the container with the ID for the current day, and add “in active” class to all other containers.

    You can copy the script inside the if(hash != ""){} block to manage the classes, assigning the correct ID to hash. Thus all you really need is the switch/case to determine the correct ID from day of week.

    This could also be managed with PHP if you prefer. You’d need to locate the code responsible for outputting the tab container’s classes. When the container with the ID for the current day is output, the class should be “active”, in all other cases it should be “in active”.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Set default/active tab by day’ is closed to new replies.