In my latest Wordpress project I'm trying to figure out how to use JQuery tabs in combination with the ACF repeater fields. The first one works but now I want multiple divs containing a few tabs on a single page. The following ones don't show the content in tabs (the script is not working on these). As far as I understand this is caused by the ID of the div (#tabs). Therefore I like to add something to this ID to see if this fixes my problem; like #tabs1, #tabs2, #tabs3, etc. I can't give each div it's own ID manually as they are created in a loop (ACF repeater field). Been searching for days and can't seem to find how to do this in the script. Hope someone can guide me in the right direction.
<script>
jQuery(document).ready(function($) {
$('#tabs').tabs();
})
</script>
The loop to create the tabs:
<?php if( have_rows('slider') ): ?>
<div id="tabs">
<?php while(have_rows('slider') ): the_row(); ?>
<div id="<?php the_sub_field('tab_title');?>">
<?php the_sub_field('tab_content');?>
</div>
<?php endwhile; ?>
<ul>
<?php while(have_rows('slider') ): the_row(); ?>
<li><?php the_sub_field('tab_title'); ?></li>
<?php endwhile; ?>
</ul>
</div>
<?php endif; ?>
Here's an example of a website that has a set up kind of like what I'm trying to achieve... note that each section has a slide show with buttons (in my case the 'tabs') underneath to select a different slide show.
http://partnersandspade.com/studio/
I believe the problem lies here <div id="<?php the_sub_field('tab_title');?>">.
If your tab_title have spaces, comma, quotes (which is most likely since it's a title and not a slug) and you use it as an element id then this isn't valid and will likely cause some problem.
Use simple ids instead. tab-1, tab-2, tab-2 etc.,
e.g.
<?php if( have_rows('slider') ): ?>
<div id="tabs">
<?php $i = 1; ?> <!-- initiate the loop count var -->
<?php while(have_rows('slider') ): the_row(); ?>
<div id="#tab-<?php echo $i;?>">
<?php the_sub_field('tab_content');?>
</div>
<?php $i++; ?>
<?php endwhile; ?>
<ul>
<?php $i = 1; ?> <!-- initiate the loop count var -->
<?php while(have_rows('slider') ): the_row(); ?>
<li><?php the_sub_field('tab_title'); ?></li>
<?php $i++; ?>
<?php endwhile; ?>
</ul>
</div>
<?php endif; ?>
UPDATE
Changing to class should work but if it doesn't I would suggest to change your ACF structure a bit. You could include the slider repeater in another repeater field "tabs" then you would be able to loop in the loop (docs - nested lops) and that way you will give each "tabs" an unique id. See the code below:
<?php if( have_rows('tabs') ): ?>
<?php $i = 1; while(have_rows('tabs') ): the_row(); ?>
<div id="tabs<?php echo $i++; ?>">
<?php while(have_rows('slider') ): the_row(); ?>
<div id="<?php the_sub_field('tab_title');?>">
<?php the_sub_field('tab_content');?>
</div>
<?php endwhile; ?>
<ul>
<?php while(have_rows('slider') ): the_row(); ?>
<li><?php the_sub_field('tab_title'); ?></li>
<?php endwhile; ?>
</ul>
</div>
<?php endwhile; ?>
<?php endif; ?>
I've misunderstood at the first time. Did you consider changing id="tabs" to class="tabs". This should solve the problem:
<script>
jQuery(document).ready(function($) {
$('.tabs').tabs();
})
</script>
Markup:
<div class="tabs">
IGONRE BELOW:
Add variable $i with value "1" and increase its value on the end on the loop.
Replace
<?php the_sub_field('tab_title');?>
with
tabs<?php echo $i;?>
All together this should give you unique id for each row.
<?php $i = 1; while(have_rows('slider') ): the_row(); ?>
<div id="tabs<?php echo $i;?>">
<?php the_sub_field('tab_content');?>
</div>
<?php $i++; endwhile; ?>
This is where I'm at now...
I managed to give all tab wrappers (#tabs) it's own ID and all the tabs inside this wrapper will get a unique ID too.
<?php $tabsId = 1; while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class( 'cf' ); ?> role="article">
<header class="article-header">
<h1 class="h2 entry-title"><?php the_title(); ?></h1>
</header>
<?php if( have_rows('slider') ): ?>
<div id="tabs<?php echo $tabsId; ?>">
<?php $tabId = 1; while(have_rows('slider') ): the_row(); ?>
<div id="tab<?php echo $tabId;?>">
<?php the_sub_field('tab_content');?>
</div>
<?php $tabId++; endwhile; ?>
<ul class="tabs">
<?php $tabId = 1; while(have_rows('slider') ): the_row(); ?>
<li><?php the_sub_field('tab_title'); ?></li>
<?php $tabId++; endwhile; ?>
</ul>
</div>
<?php endif; ?>
<section class="entry-content cf">
<?php the_content(); ?>
</section>
</article>
<?php $tabsId++; endwhile; ?>
However I think I need to change the script somehow so it knows that I'm not targeting the #tabs div but I'm targeting #tabs1, #tabs2, #tabs3 etc. Here's the script I'm using as i'm using it. Is there way to change this so it runs on each #tabs.. div?
jQuery(document).ready(function($) {
$('#tabs1').each(function(){
// For each set of tabs, we want to keep track of
// which tab is active and it's associated content
var $active, $content, $links = $(this).find('a');
// If the location.hash matches one of the links, use that as the active tab.
// If no match is found, use the first link as the initial active tab.
$active = $($links.filter('[href="'+location.hash+'"]')[0] || $links[0]);
$active.addClass('active');
$content = $($active[0].hash);
// Hide the remaining content
$links.not($active).each(function () {
$(this.hash).hide();
});
// Bind the click event handler
$(this).on('click', 'a', function(e){
// Make the old tab inactive.
$active.removeClass('active');
$content.hide();
// Update the variables with the new link and content
$active = $(this);
$content = $(this.hash);
// Make the tab active.
$active.addClass('active');
$content.show();
// Prevent the anchor's default click action
e.preventDefault();
});
});
})
Related
I have to add a Slick slider with ACF repeating fields to MaxMegaMenu, unfortunately I don't know how to add such a large piece of code via Javascript (passing js Var?). Has anyone tried to add ACF repeater using Javascript?
My code below
++++++++++++++++
<?php if ( have_rows('postgraduate_studies', 'option') ) :
while( have_rows('postgraduate_studies', 'option') ) : the_row(); ?>
<?php the_sub_field('sub_field_name', 'option'); ?>
<div style="background-image: url( <?php the_sub_field('postgraduate_studies_image', 'option'); ?>);" class="">
<a href="<?php the_sub_field('postgraduate_studies_link', 'option'); ?>">
<p><?php the_sub_field('postgraduate_studies_text', 'option'); ?></p>
</a>
</div>
<?php endwhile; ?>
<?php endif; ?>
Hi im basically creating a help page, where there is a dynamic list created using the repeater field.
Each list item there is a line of text, that when clicked on should show some more content (and hide it when clicked on again).
Now i can get it to work for the first item in the list… but every new item only shows/hides the first item.
Im assuming i need to do some for each for the id or something i just don't know how to.
Thx for any help, here is the code:
<?php if (have_rows('video_felt')): ?>
<ul>
<?php while (have_rows ('video_felt')): the_row();
//vars
$afsnitOverskrift = get_sub_field('afsnit_titel');
?>
<li>
<?php if($afsnitOverskrift): ?>
<?php echo $afsnitOverskrift; ?>
<?php endif;?>
</li>
<?php if (have_rows('video_liste')): ?>
<ul>
<?php while (have_rows ('video_liste')): the_row();
//vars
$videListe = get_sub_field('video_navn');
$videoLinker = get_sub_field('vide_link');?>
<li onclick="myFunction()">
<?php if($videListe): ?>
<a ><?php echo $videListe; ?></a>
<div id="myDIV" style="display:none;">
<div class="youtube-player" data-id="<?php echo $videoLinker;?>"></div>
</div>
<?php endif;?>
</li>
<?php endwhile;?>
</ul>
<?php endif;?>
<?php endwhile; ?>
</ul>
<?php endif;?>
<script>
function myFunction() {
var x = document.getElementById("myDIV");
if (x.style.display === "block") {
x.style.display = "none";
} else {
x.style.display = "block";
}
}
</script>
I just edit your code with jquery kindly check below working code.
<?php if (have_rows('video_felt')): ?>
<ul class="videoul">
<?php while (have_rows ('video_felt')): the_row();
//vars
$afsnitOverskrift = get_sub_field('afsnit_titel');
?>
<li>
<?php if($afsnitOverskrift): ?>
<?php echo $afsnitOverskrift; ?>
<?php endif;?>
</li>
<?php if (have_rows('video_liste')): ?>
<ul>
<?php while (have_rows ('video_liste')): the_row();
//vars
$videListe = get_sub_field('video_navn');
$videoLinker = get_sub_field('vide_link');?>
<li >
<?php if($videListe): ?>
<a ><?php echo $videListe; ?></a>
<div class="myDIV" style="display:none;">
<div class="youtube-player" data-id="<?php echo $videoLinker;?>"></div>
</div>
<?php endif;?>
</li>
<?php endwhile;?>
</ul>
<?php endif;?>
<?php endwhile; ?>
</ul>
<?php endif;?>
jQuery
<script>
jQuery(document).ready(function(){
jQuery('body').on('click', 'ul.videoul li', function() {
jQuery("div.myDIV").hide();
jQuery(this).find("div.myDIV").toggle();
});
});
</script>
After hours of research and try and errors I start my first question here.
If it can be done with wordpress syntax even better. Otherwise I think I'm near to the solution.
I want a custom gallery on a wordpress template page but with the post image from certain categories with the content from it. So far so good.
In my designerin.php template I give every images a class img-x.
class="img-<?php echo $img_counter; ?>
And the content get the id img-x
id="img-<?php echo $post_counter; ?>
The counter is just an increment. So image 1 has class img-1, image 2 img-2 and so on.
Now I want the related content of that image created in the wordpress editor showing up when you are clicking on this specific image.
Say you click on image 5 and want the content from image 5.
I'm stucked on the step where I say (in jQuery), that display the content
if .img-x == #img-x {
display the_content()
}
Enough said - here is my jQuery code:
$(".large-img-wrapper").hide();
$('.gallery li').on('click', function(e) {
e.preventDefault();
var largeImgLink = $(this).find('a').attr('href');
$(".large-img-wrapper").first().fadeIn();
var imgContent = $('#img-5').html();
$('#large-img').remove();
$('#append-id').remove();
$(".large-img").append('<img id="large-img" src="' + largeImgLink + '">').fadeIn();
$(".large-img").append('<div id="append-id">' + imgContent + '</div>').fadeIn();
$(".large-img-container .img-title").remove();
});
The var imgContent was just a test. Every image shows up the content from image 5 at the moment.
My template page code:
<div class="row content full-width">
<div class="cats row">
Alle Kategorien
<?php query_posts( 'category_name=designerin&posts_per_page=-1'); if (have_posts()) : while (have_posts()) : the_post();
$categories = get_the_category();
$separator = ' ';
$output = '';
if($categories){
foreach($categories as $category) {
$output .= $category->cat_name;
}
}
$cat_name = get_category(get_query_var('cat'))->name;
?>
<?php echo trim($output); ?>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
</div>
<ul class="row small-block-grid-3 medium-block-grid-5 large-block-grid-8 gallery">
<?php $img_counter = 0; ?>
<?php query_posts( 'category_name=designerin&posts_per_page=-1'); if (have_posts()) : while (have_posts()) : the_post();
$img_counter++;
$thumb_id = get_post_thumbnail_id();
$thumb_url_array = wp_get_attachment_image_src($thumb_id, 'thumbnail', true);
$large_url_array = wp_get_attachment_image_src($thumb_id, 'large', true);
$thumb_url = $thumb_url_array[0];
$large_url = $large_url_array[0];
$categories = get_the_category();
$separator = ' ';
$output = '';
if($categories){
foreach($categories as $category) {
$output .= $category->cat_name;
}
}
?>
<li>
**<img src="<?php echo $thumb_url; ?>" alt="<?php echo the_title(); ?>">**
</li>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
</ul>
<?php $post_counter = 0; ?>
<?php query_posts( 'category_name=designerin&posts_per_page=-1'); if (have_posts()) : while (have_posts()) : the_post();
$post_counter++;
?>
**<ul class="large-img-wrapper">
<li>
<div class="large-img-container">
<div class="large-img"></div>
<div class="img-content" id="img-<?php echo $post_counter; ?>">
<div class="img-title">
<?php echo the_title(); ?>
</div>
<div class="img-text">
<?php echo the_content(); ?>
</div>
</div>
</div>
</li>
</ul>**
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
</div>
The first query is for filtering the categories by slug.
My last attempt feels so near..
var inc = '5';
var objClass = $(this).find("a").hasClass( "img-" + inc );
console.log(objClass);
When I'm clicking on the 5th image I get "true". But how can I get the X (inc) related to my template php code?
Sorry for my english and possible bad code
If i'm understanding correctly this might help
$('.gallery li').on('click', function(e) {
e.preventDefault();
var className = $(this).find('a').attr('class');
var imgContent = $('#'+className).html();
});
This is my wp loop:
<?php get_header(); ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>
<div style="background:url(<?php echo $url; ?>);">
<button id="change">preview</button>
</div>
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>
<?php get_footer(); ?>
After clicking on button "change" it should change my body background to $url for e.g.
<body style="background:url($url);
where $url is thumbnail url
Any ideas how to make it?
Greetings
EDIT: I noticed you are using id="change". Id is unique and if your loop makes more than one button with the same Id, it won't work. make class="change". I've edited the rest for you.
here is a jsfiddle. http://jsfiddle.net/Grimbode/2bt8Q/1/
You should essentially load your url's as the value of a hidden input, like so:
<body>
<div>
<button class="change">preview<input type="hidden" value="http://pngimg.com/upload/tree_PNG2517.png"/></button>
</div>
</body>
Then using jQuery:
$('.change').on('click', function(){
$(document.body).css('background', 'url('+$(this).find('input').val()+')');
});
When the button is clicked, it will change your document.body's background to whatever the URL is.
Thus your php file should look like this:
<?php get_header(); ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>
<div>
<button class="change">preview<input type="hidden" value="<?php echo $url; ?>"/></button>
</div>
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>
<?php get_footer(); ?>
Hope this helps, cheers.
I have list of categories, some have sub-category and some have sub-sub-category which are in such a way:
Main menu
sub menu
sub menu
I used ddaccordian.init to watch for open close events. All works fine, But now what I want is that current selected URL category must remain open.
I have tried the following code:
<script type="text/javascript">
ddaccordion.init({
headerclass: "question", //Shared CSS class name of headers group
contentclass: "theanswer", //Shared CSS class name of contents group
revealtype: "click", //Reveal content when user clicks or onmouseover the header? Valid value: "click", "clickgo", or "mouseover"
mouseoverdelay: 200, //if revealtype="mouseover", set delay in milliseconds before header expands onMouseover
collapseprev: false, //Collapse previous content (so only one open at any time)? true/false
defaultexpanded: [], //index of content(s) open by default [index1, index2, etc]. [] denotes no content.
onemustopen: false, //Specify whether at least one header should be open always (so never all headers closed)
animatedefault: false, //Should contents open by default be animated into view?
persiststate: false, //persist state of opened contents within browser session?
toggleclass: ["closedanswer", "openanswer"], //Two CSS classes to be applied to the header when it's collapsed and expanded, respectively ["class1", "class2"]
//togglehtml: ["prefix", "<img src='images/plus.gif' /> ", "<img src='images/minus.gif' />"], //Additional HTML added to the header when it's collapsed and expanded, respectively ["position", "html1", "html2"] (see docs)
animatespeed: "fast", //speed of animation: integer in milliseconds (ie: 200), or keywords "fast", "normal", or "slow"
oninit:function(expandedindices){ //custom code to run when headers have initalized
//do nothing
},
onopenclose:function(header, index, state, isuseractivated){ //custom code to run whenever a header is opened or closed
//do nothing
}
})
</script>
and to get the current category activated i.e. must remain open I did this:
<a <?php if($_category->hasChildren()): ?>class="question"<?php endif; ?><?php if ($currentUrl == $this->getCategoryUrl($_category)): ?> class="current"<?php endif; ?> href="<?php echo $this->getCategoryUrl($_category) ?>">
<?php echo $_category['name']; ?>
</a>
<?php $potential1 = $_category->hasChildren(); ?>
<?php if($_category->hasChildren()): ?>
<?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
<?php $_subcategories = $_category->getChildrenCategories() ?>
<ul <?php if($_subcategories): ?>class="theanswer"<?php endif; ?>>
<?php foreach($_subcategories as $subcat): ?>
<li>
<a <?php if($subcat->hasChildren()): ?>class="question"<?php endif; ?><?php if ($currentUrl == $this->getCategoryUrl($subcat)): ?> class="current"<?php endif; ?> href="<?php echo $this->getCategoryUrl($subcat); ?>">
<?php echo $subcat->getName(); ?>
</a>
<?php if($subcat->hasChildren()): ?>
<?php
$_subcat = Mage::getModel('catalog/category')->load($subcat->getId());
$childrens = $_subcat->getChildrenCategories();
?>
<ul <?php if($childrens): ?>class="theanswer"<?php endif; ?>>
<?php foreach($childrens as $_childrens): ?>
<li>
<a <?php if ($currentUrl == $this->getCategoryUrl($_childrens)): ?> class="current"<?php endif; ?>href="<?php echo $this->getCategoryUrl($_childrens); ?>">
<?php echo $_childrens->getName(); ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif ?>
</li>
<?php endforeach ?>
</ul>
<?php endif ?>
</li>
But I am getting no response on frontend :( Never mind if you think I am missing a foreach loop etc.
I have just pasted a portion of the code so tell me where I am wrong :(
Although i am getting the current URL correctly.
The accordion plugin you've chosen doesn't appear to have an option for keeping a section open so you would have to intercept that yourself and stop it. The only possible event it provides is onopenclose but that is called after slideUp has finished, which is useless.
You should pick another plugin which satisfies your needs.
Luckily Magento already comes with Prototype and it's own accordion class, you can replace or wrap it's sectionClicked method and stop the event there.
Accordion.prototype.sectionClicked = function(event) {
var element = $(Event.element(event)).up('.section');
if (!element.hasClassName('current')) {
this.openSection(element);
}
Event.stop(event);
};
This solved my problem, what i did was added this
<?php if ($this->isCategoryActive($_category)): ?>
instead of
if($currentUrl == $this->getCategoryUrl($_category)): ?> and added Css class at the bottom.
<div class="content-links">
<ul>
<?php $i=0; foreach ($catlist as $_category): ?>
<?php if($_category->getIsActive()): ?>
<li>
<a <?php if($_category->hasChildren()): ?>class="question"<?php endif; ?> href="<?php echo $this->getCategoryUrl($_category) ?>">
<?php echo $_category['name']; ?>
</a>
<?php $potential1 = $_category->hasChildren(); ?>
<?php if($_category->hasChildren()): ?>
<?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
<?php $_subcategories = $_category->getChildrenCategories() ?>
<ul class="<?php if($_subcategories): ?>theanswer<?php endif; ?><?php //if($currentUrl == $this->getCategoryUrl($_category)): ?><?php if ($this->isCategoryActive($_category)): ?> opened<?php endif; ?>">
<?php foreach($_subcategories as $subcat): ?>
<li>
<a <?php if($subcat->hasChildren()): ?>class="question"<?php endif; ?> href="<?php echo $this->getCategoryUrl($subcat); ?>">
<?php echo $subcat->getName(); ?>
</a>
<?php if($subcat->hasChildren()): ?>
<?php
$_subcat = Mage::getModel('catalog/category')->load($subcat->getId());
$childrens = $_subcat->getChildrenCategories();
?>
<ul class="<?php if($childrens): ?>theanswer<?php endif; ?><?php if ($this->isCategoryActive($_subcat)): ?> opened<?php endif; ?>">
<?php foreach($childrens as $_childrens):
?>
<li>
<a <?php if ($currentUrl == $this->getCategoryUrl($_childrens)): ?>class="current"<?php endif; ?>href="<?php echo $this->getCategoryUrl($_childrens); ?>">
<?php echo $_childrens->getName(); ?>
</a>
</li>
<?php
endforeach;
echo "</ul>";
?>
<?php endif ?>
</li>
<?php endforeach ?>
</ul>
<?php endif ?>
</li>
<?php endif ?>
<?php endforeach ?>
</ul>
</div>
</div>
<div class="clear" style="height:218px;"></div>
</div>
</div>
<div class="event-bot"></div>
</div>
</div> <!--inner right end here-->
<style type="text/css">
.opened { display:block !important;}
</style>