Kirasiris
Kirasiris

Reputation: 550

How to set active class to tab when there is more than one in Wordpress?

Is there a solution to make the custom fields content from Wordpress to be shown on their corresponding tab? I used the same function for my li elements and it worked as I wanted, the problem is when I try to do the same with tabs.

This obviously works by adding the active class to the tab, but so far it is not working even when I made the variable $class = 0(which function is to add the active class) . Can you help me with this,please ?

<!-- Tabs -->
<ul class="nav nav-pills nav-justified" id="myTab" role="tablist">
<?php $links = get_post_custom_values( 'Iframe' ); ?>
<?php foreach ($links as $li) : $class = 0 ? 'active ' : ''; ?>
<li class="nav-item <?php $class ?>"><a class="nav-link " id="tab" data-toggle="tab" href="#tab" role="tab" aria-controls="tab" aria-expanded="true">Option</a></li>
<?php endforeach ; ?>    
</ul>
<div class="tab-content" id="myTabContent">
<!--------------------------------------------------------- Youtube player --------------------------------------------------------------------------------->
<?php $player = get_post_custom_values( 'Iframe' ); ?>
<?php foreach ($player as $iframe) :  $class = 0  ? 'active ' : ''; ?>
<div class="tab-pane active" id="#tab" role="tabpanel" aria-labelledby="tab">
<br>
<div class="song">
<div class="video-grid">
<?php echo $iframe; ?>
</div>
</div>
</div>
<?php endforeach ?> 

This is what it looks like in the front-end: As you can see the content is shown in just one tab

I tried with Javascript, but still did not work:

$(document).ready(function() {
    $(".tab-pane").click(function() {
        if ($("#tab").hasClass("active")) {
            $("#tab").removeClass("active");
            $("#tab").addClass("active");
        } 
    });
    $(".tab-pane").click();
});

Upvotes: 0

Views: 1830

Answers (1)

madalinivascu
madalinivascu

Reputation: 32354

You need a unique id for each tab for your tab to work

<!-- Tabs -->
<ul class="nav nav-pills nav-justified" id="myTab" role="tablist">
<?php $links = get_post_custom_values( 'Iframe' ); ?>
<?php foreach ($links as $key=>$li) :  ?>
<li class="nav-item <?php echo $key == 0 ? 'active ' : ''; ?>"><a class="nav-link " data-toggle="tab" href="#tab<?php echo $key; ?>" role="tab" aria-controls="tab" aria-expanded="true">Option</a></li>
<?php endforeach ; ?>    
</ul>
<div class="tab-content" id="myTabContent">
<!--------------------------------------------------------- Youtube player --------------------------------------------------------------------------------->
<?php $player = get_post_custom_values( 'Iframe' ); ?>
<?php foreach ($player as $key=>$iframe) : ?>
<div class="tab-pane <?php echo $key == 0 ? 'active ' : ''; ?>" id="tab<?php echo $key; ?>" role="tabpanel" aria-labelledby="tab">
<br>
<div class="song">
<div class="video-grid">
<?php echo $iframe; ?>
</div>
</div>
</div>
<?php endforeach ?> 

see compiled version

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<!-- Tabs -->
<ul class="nav nav-pills nav-justified" id="myTab" role="tablist">
<li class="nav-item active"><a class="nav-link " data-toggle="tab" href="#tab0" role="tab" aria-controls="tab" aria-expanded="true">Option</a></li>
<li class="nav-item "><a class="nav-link " data-toggle="tab" href="#tab1" role="tab" aria-controls="tab" aria-expanded="true">Option</a></li>
<li class="nav-item "><a class="nav-link " data-toggle="tab" href="#tab2" role="tab" aria-controls="tab" aria-expanded="true">Option</a></li>
    
</ul>
<div class="tab-content" id="myTabContent">
<!--------------------------------------------------------- Youtube player --------------------------------------------------------------------------------->
<div class="tab-pane active" id="tab0" role="tabpanel" aria-labelledby="tab">
<br>
<div class="song">
<div class="video-grid">
<iframe src="https://player.vimeo.com/video/229734145" width="640" height="360" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
<p><a href="https://vimeo.com/229734145">Bleach Ending 30 full</a> from <a href="https://vimeo.com/user49994421">World Anime</a> on <a href="https://vimeo.com">Vimeo</a>.</p></div>
</div>
</div>
<div class="tab-pane " id="tab1" role="tabpanel" aria-labelledby="tab">
<br>
<div class="song">
<div class="video-grid">
<iframe width="560" height="315" src="https://www.youtube.com/embed/zjnGJrJ-D6Y" frameborder="0" allowfullscreen></iframe></div>
</div>
</div>
<div class="tab-pane " id="tab2" role="tabpanel" aria-labelledby="tab">
<br>
<div class="song">
<div class="video-grid">
<iframe width="560" height="315" src="https://www.youtube.com/embed/nGdFHJXciAQ" frameborder="0" allowfullscreen></iframe></div>
</div>
</div>

Upvotes: 1

Related Questions