Give “active” active class to a tab in html if there’s a value in a variable
-
Hello guys,
I’ve been working in a new template in WordPress, and I’ve been using the WordPress custom fields.
What I’m doing is inserting an URL to each custom field, if there is not URL, by default the value for them is “#”.
So when I put a Youtube URL it makes the tab “active” and it works well, the problem is when I only insert a Vimeo URL, the tabs is not shown as active; the only way to active it is by the user clicking on it. Is it possible to change this with CSS, or Javascript?<!-- Tabs --> <?php $youtube_url = get_post_meta($post->ID, 'Youtube URL', true); ?> <?php $vimeo_url = get_post_meta($post->ID, 'Vimeo URL', true); ?> <ul class="nav nav-pills nav-justified" id="myTab" role="tablist"> <?php if ($youtube_url=='#') : ?> <?php else : ?> <li class="nav-item active"><a class="nav-link " id="youtube-tab" data-toggle="tab" href="#youtube" role="tab" aria-controls="youtube" aria-expanded="true">Youtube</a></li> <?php endif; ?> <?php if ($vimeo_url== '#') : ?> <?php else : ?> <li class="nav-item"><a class="nav-link " id="vimeo-tab" data-toggle="tab" href="#vimeo" role="tab" aria-controls="vimeo">Vimeo</a></li> <?php endif; ?> </ul> <div class="tab-content" id="myTabContent"> <!--------------------------------------------------------- Youtube player ---------------------------------------------------------------------------------> <?php if ($youtube_url=='#') : ?> <?php else : ?> <div class="tab-pane active" id="youtube" role="tabpanel" aria-labelledby="youtube-tab"> <br> <div class="song"> <div class="video-grid"> <iframe src="https://www.youtube.com/embed/<?php echo youtube_url(get_the_ID()); ?>" allowfullscreen></iframe> </div> </div> <div class="song-grid-right"> <div class="share"> <h5>Share this</h5> <ul> <li><a rel="nofollow" href="https://www.facebook.com/sharer.php?u=<?php the_permalink();?>&t=<?php echo urlencode(get_the_title($id)); ?>"><i class="fa fa-facebook-official" aria-hidden="true"></i> Facebook</a></li> <li><a rel="nofollow" href="https://plus.google.com/share?url=<?php the_permalink();?>"><i class="fa fa-google-plus" aria-hidden="true"></i> Google</a></li> <li><a rel="nofollow" href="https://twitter.com/share?text=<?php the_title(); ?>&url=<?php the_permalink(); ?>"><i class="fa fa-twitter" aria-hidden="true"></i> Twitter</a></li> <li><a rel="nofollow" href="https://pinterest.com/pin/create/button/?url=<?php the_permalink(); ?>&description=<?php the_title(); ?>"><i class="fa fa-pinterest" aria-hidden="true"></i> Pinterest</a></li> <li><a rel="nofollow" href="whatsapp://send?text=<?php the_permalink(); ?>"> Whatsapp</a></li> <li><a href="#comments" class="icon comment-icon"><i class="fa fa-comments" aria-hidden="true"></i> <?php comments_number( 'no responses', 'one response', '% responses' ); ?></a></li> <li><i class="fa fa-eye" aria-hidden="true"></i> <?php echo wpb_get_post_views(get_the_ID()); ?></li> </ul> </div> </div> <div class="clearfix"> </div> </div> <?php endif; ?> <?php if ($vimeo_url=='#') : ?> <?php else : ?> <div class="tab-pane" id="vimeo" role="tabpanel" aria-labelledby="vimeo-tab"> <br> <div class="song"> <div class="video-grid"> <iframe src="https://player.vimeo.com/video/<?php echo vimeo_url(get_the_ID()); ?>" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe> </div> </div> <div class="song-grid-right"> <div class="share"> <h5>Share this</h5> <ul> <li><a rel="nofollow" href="https://www.facebook.com/sharer.php?u=<?php the_permalink();?>&t=<?php echo urlencode(get_the_title($id)); ?>"><i class="fa fa-facebook-official" aria-hidden="true"></i> Facebook</a></li> <li><a rel="nofollow" href="https://plus.google.com/share?url=<?php the_permalink();?>"><i class="fa fa-google-plus" aria-hidden="true"></i> Google</a></li> <li><a rel="nofollow" href="https://twitter.com/share?text=<?php the_title(); ?>&url=<?php the_permalink(); ?>"><i class="fa fa-twitter" aria-hidden="true"></i> Twitter</a></li> <li><a rel="nofollow" href="https://pinterest.com/pin/create/button/?url=<?php the_permalink(); ?>&description=<?php the_title(); ?>"><i class="fa fa-pinterest" aria-hidden="true"></i> Pinterest</a></li> <li><a rel="nofollow" href="whatsapp://send?text=<?php the_permalink(); ?>"> Whatsapp</a></li> <li><a href="#comments" class="icon comment-icon"><i class="fa fa-comments" aria-hidden="true"></i> <?php comments_number( 'no responses', 'one response', '% responses' ); ?></a></li> <li><i class="fa fa-eye" aria-hidden="true"></i> <?php echo wpb_get_post_views(get_the_ID()); ?></li> </ul> </div> </div> <div class="clearfix"> </div> </div> <?php endif; ?>
The page I need help with: [log in to see the link]
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘Give “active” active class to a tab in html if there’s a value in a variable’ is closed to new replies.