How to show a hierarchical terms list in tabs - wordpress

I'm trying to display a list of taxonomy terms in bootstrap tabs.
Listing the terms as below, but need some help to assign into the tabs.
Using #dipmala 's guide, I came upto following. But still can't figure out a method to separate child terms into relevant tabs. Currently the issue is all inside the foreach loop.
<?php $sup_terms = get_terms("supplier_categoties", array("orderby" => "slug", "parent" => 0, "hide_empty" => false)); ?>
<ul class="nav nav-tabs sup_cats" role="tablist">
<?php foreach($sup_terms as $key => $sup_term) : ?>
<li role="presentation">
<?php echo $sup_term->name; ?>
</li>
<script type="text/javascript">
jQuery(".sup_cats li:first").addClass("active");
</script>
<?php
$sup_c_terms = get_terms("supplier_categoties", array("orderby" => "slug", "parent" => $sup_term->term_id, "hide_empty" => false));
if($sup_c_terms) :
foreach($sup_c_terms as $key => $sup_c_term) :
$tabcontent .='<div role="tabpanel" class="tab-pane" id="'.$sup_term->slug.'">';
$tabcontent .= $sup_c_term->name;
$tabcontent .='</div>';
endforeach;
endif;
endforeach;
?></ul>
<div class="tab-content sup_cats_inner">
<div role="tabpanel" class="tab-pane" id="<?php $sup_term->slug ?>">
<?php echo $tabcontent; ?>
<script type="text/javascript">
jQuery('.sup_cats_inner > :first-child').addClass("active");
</script>

Following code will helpful.
<?php $sup_terms = get_terms("supplier_categoties", array("orderby" => "slug", "parent" => 0, "hide_empty" => false)); ?>
<!-- Nav tabs -->
<ul class="nav nav-tabs" role="tablist">
<?php foreach($sup_terms as $key => $sup_term) : ?>
<li role="presentation" class="active"><?php echo $sup_term->name; ?></li>
<?php
$tabcontent .=' <div role="tabpanel" class="tab-pane active" id="'.$sup_term->slug.'">...</div>'; // dynamic tab content
endforeach; ?> </ul>
<!-- Tab panes -->
<div class="tab-content">
<?php echo $tabcontent; ?>
</div>

Related

Create responsive tabs with content from post in wordpress

I am new in Wordpress so I hope I will be very explicit in the following description. Right now I`m trying to create something like https://getbootstrap.com/docs/5.1/components/navs-tabs/ but in Wordpress using bootstrap 5. In the tabs title I want to show the title of the post and in the tab content will be the_content().Thanks in advance !
The ul needs to be outside of the while loop so that you only have a single navigation component. Right now it is creating a separate navigation for every oracion. Try the following:
<div class="col-md-9">
<?php
$wpb_all_query = new WP_Query(array(
'post_type' => 'post',
'category_name' => 'orar',
'post_status' => 'publish',
)); ?>
<?php if ($wpb_all_query->have_posts()) : ?>
<ul class="nav nav-pills mb-3" id="pills-tab" role="tablist">
<?php while ($wpb_all_query->have_posts()) :
$wpb_all_query->the_post(); ?>
<li class="nav-item" role="presentation">
<button class="nav-link" id="pills-profile-tab" data-bs-toggle="pill" data-bs-target="#pills-profile" type="button" role="tab" aria-controls="pills-profile" aria-selected="false"><?php $wpb_all_query->the_title(); ?></button>
</li>
<div class="tab-content" id="pills-tabContent">
<div class="tab-pane fade" id="pills-profile" role="tabpanel" aria-labelledby="pills-profile-tab"><?php $wpb_all_query->the_content(); ?></div>
</div>
<?php endwhile; ?>
</ul>
<?php wp_reset_postdata(); ?>
<?php
else : ?>
<p><?php ('Sorry, no posts matched your criteria.'); ?></p>
<?php
endif; ?>
</div>

Show parent child taxonomy in nested tabs with its post

I want to show parent taxonomy in tab section and the under parent taxonomy i want to display child taxonomy in nested tab. On selection of child taxonomy i want to show its post. I am getting every time child taxonomy with with its post which i don't want.
Here is what i want:
TAB1(parent active) TAB2(parent)
ALL_TAB1(child active) SUB1_TAB1 SUB2_TAB1
(posts for all, sub1_tab1, sub2_tab1 and respectively whichever is active)
What i tried
<?php
/*
Template Name: Prac
*/
get_header();
// Get list of 'categories' for tabs -->
$args = array(
'hide_empty' => false,
'parent' => 0
);
$preferences = get_terms( 'preferences', $args );
?>
<div class="row">
<div class="col-md-12">
<?php
while ( have_posts() ) : the_post();
the_content();
endwhile; ?>
<ul class="nav nav-tabs" id="myTab" role="tablist">
<!-- Create the tabs -->
<?php
// Use counter so that 'active' class is only applied to first tab
$counter = 0;
foreach ($preferences as $preference) { ?>
<li class="nav-item">
<a class="nav-link <?= ($counter == 0) ? 'active' : '' ?>" id="<?php echo $preference->slug;?>-tab" data-toggle="tab" href="#<?php echo $preference->slug; ?>" role="tab" aria-controls="<?php echo $preference->slug;?>" aria-selected="<?= ($counter == 0) ? 'true' : 'false' ?>"><?php echo $preference->name; ?></a>
</li>
<?php $counter++; } ?>
</ul>
<div class="tab-content" id="nav-tabContent">
<!-- Get the content for each tab -->
<?php
$counter2 = 0;
foreach ($preferences as $preference) { ?>
<div role="tabpanel" class="tab-pane fade <?= ($counter2 == 0) ? 'show active' : '' ?>" id="<?php echo $preference->slug; ?>" aria-labelledby="<?php echo $preference->slug; ?>-tab">
<div class="row">
<?php
$args = array(
'post_type' => 'project',
'tax_query' => array(
array(
'taxonomy' => $preference->taxonomy,
'field' => $preference->slug,
'terms' => $preference->term_id
)
)
);
$loop = new WP_Query( $args );
?>
<div class="col-md-6" id="<?php echo $preference->slug . '-clauses' ?>">
<?php
?>
<?php
if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php
//Do something if a specific array value exists within a post
$term_list = wp_get_post_terms($post->ID, $preference->taxonomy, array("fields" => "all"));
?>
<?php foreach($term_list as $term_single) {
$category_children = get_terms(array(
'parent' => $term_single->term_id,
'hide_empty' => false
) );
$category_children_count = count($category_children);
?>
<?php } ?>
<ul class="nav nav-tabs" role="tablist">
<li class="nav-item">
<?php echo $term_single->name; ?>
</li>
</ul>
<a class="col-md-6" href="<?php echo the_permalink();?>"><?php the_post_thumbnail();?><?php echo the_title(); ?></a>
<?php endwhile; endif; wp_reset_query(); ?>
</div>
</div> <!-- end row -->
</div> <!-- end tab-pane -->
<?php $counter2++; } ?>
</div> <!-- end tab-content -->
</div> <!-- end col -->
</div> <!-- end row -->
<!-- end content -->
<?php get_footer(); ?>
I am getting like this
TAB1 TAB2
SUB1_TAB1
post
SUB1_TAB1
post
SUB1_TAB2
Can anyone help ? Thanks.

Wordpress: No search results message

I've tried to separate search results by categories in 2 tabs. Everything works fine except showing the message "No results". This message is shown only when in both categories are nothing found. But when one tab has results and another hasn't - nothing is shown.
I'm looking for way to show "No results" for every tab. I mean, if nothing is found in cat 1 and some results found in cat 2 -> Show "No results" in Tab 1 and show results in Tab 2.
Any suggestions?
Code here:
<div id="tab-content1" class="tab-content">
<ul class="posts--group v-category-games">
<?php
global $post;
rewind_posts();
$query = new WP_Query(array(
'posts_per_page' => -1,
'cat' => 3,
's' => get_search_query(),
));
if (have_posts()) :
while ($query->have_posts()) : $query->the_post();
?>
<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>
<li>
<div class="post-item v-category-games v-with-image">
<div class="post-item--text">
<a class="post-item--text--name" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<span class="post-item--text--tagline">
<span><?php the_excerpt(); ?> </span>
<span> </span>
</span>
</div>
<div class="post-item--thumbnail">
<div class="post-thumbnail">
<div class="backgroundImage_1hK9M post-thumbnail--image" style="background-image: url('<?php echo $url; ?>');"></div>
<span></span>
<span></span>
</div>
</div>
</div>
</li>
<?php
endwhile;
?>
<?php
else :
echo "<div class='art_descr'><h2>No results in this category!</h2></div></center>";
endif;
wp_reset_postdata();
?>
</ul>
</div> <!-- #tab-content1 -->
<div id="tab-content2" class="tab-content">
<ul class="posts--group v-category-games">
<?php
global $post;
rewind_posts();
$query = new WP_Query(array(
'posts_per_page' => -1,
'cat' => 4,
's' => get_search_query(),
));
if (have_posts()) :
while ($query->have_posts()) : $query->the_post();
?>
<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>
<li>
<div class="post-item v-category-games v-with-image">
<div class="post-item--circle">
<?php the_field('digest_number'); ?>
</div>
<div class="post-item--text no-margin">
<a class="post-item--text--name" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<span class="post-item--text--tagline">
<span><p><?php the_field('short_description'); ?></p> </span>
<span> </span>
</span>
</div>
</div>
</li>
<?php
endwhile;
?>
<?php
else :
echo "<div class='art_descr'><h2>No results in this category!</h2></div></center>";
endif;
wp_reset_postdata();
?>
</ul>
</div> <!-- #tab-content2 -->
</div>
You can use your if (have_posts()) : on each loop to determine if to display or not...
if (have_posts()) :
//while loop
else:
echo '<h1>no posts found</h1>';
endif;

how to write href Men Category link in wordpress?

For my WordPress theme I need help from an expert. Here is my nav-list html
Example:
//Here is my WP code:
<li>
<?php
$category_men = get_the_category_by_id($catmen['content-cat-one']);
$content_one =new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => 1,
'category_name' => $category_men
));
while($content_one->have_posts()) : $content_one->the_post(); ?>
<a href="<?php echo get_category_link( $catmen['content-cat-one'] ); ?>#<?php echo get_cat_ID($category_men); ?>" >Men Category</a>
<?php endwhile; ?>
</li>
<ul class="nav nav-tabs">
<li>Men Category</li>
</ul>
<div class="tab-pane active" id="men">
</div>
<div class="tab-pane active" id="<?php echo get_cat_ID($category_men); ?>">
The result comes out: //localhost/my-themes/category/featured/#5
I do not want the result above, I want //localhost/my-themes/category/featured/
How to overcome these problems?
Replace this code,
<a href="<?php echo get_category_link( $catmen['content-cat-one'] ); ?>#<?php echo get_cat_ID($category_men); ?>" >Men Category</a>
with the code snippet bellow.
Men Category

WordPress: Get Post Thumbnail Outside of Loop

I am trying to get the post thumbnail image for my carousel thumbnail indicators which are outside of the loop.
Currently it's setup with image placeholders but I need each thumb to represent the currently selected post.
<?php
$items = new WP_Query(array(
'post__in' => get_option('sticky_posts'),
'caller_get_posts' => 1,
'posts_per_page' => 10,
'meta_key' => '_thumbnail_id'
));
$count = $items->found_posts;
?>
<div id="myCarousel" class="carousel slide" data-ride="carousel">
<!-- Wrapper for slides -->
<div class="carousel-inner">
<?php
$ctr = 0;
while ( $items->have_posts() ) :
$items->the_post();
$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
$custom = get_post_custom($post->ID);
$link = $custom["more-link"][0];
$class = $ctr == 0 ? ' active' : '';
?>
<div class="item<?php echo $class; ?>" id="<? the_ID(); ?>">
<?php the_post_thumbnail( 'full', array (
'class' => 'img-responsive'
)); ?>
<div class="carousel-caption">
<h3><a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a></h3>
</div>
</div>
<!-- End Item -->
<?php $ctr++;
endwhile; ?>
</div>
<!-- End Carousel Inner -->
<div class="thumbs">
<div class="row">
<div class="col-md-2 active item" data-target="#myCarousel" data-slide-to="0"><img src="http://placehold.it/1200x440/999999/cccccc" class="img-responsive"></div>
<?php for($num = 1; $num < $count; $num++){ ?>
<div class="col-md-2 item" data-target="#myCarousel" data-slide-to="<?php echo $num; ?>"><img src="http://placehold.it/1200x440/999999/cccccc" class="img-responsive"></div>
<?php } ?>
</div>
</div>
</div>
<!-- End Carousel -->
Instead of:
<?php for($num = 1; $num < $count; $num++){ ?>
<div class="col-md-2 item" data-target="#myCarousel" data-slide-to="<?php echo $num; ?>">
<a href="#">
<img src="http://placehold.it/1200x440/999999/cccccc" class="img-responsive">
</a>
</div>
<?php } ?>
try using:
<?php $counter2 = 0; ?>
<?php while ( $items->have_posts() ) : $items->the_post(); ?>
<?php $counter2++; ?>
<div class="col-md-2 item <?php echo $counter2 == 1 ? 'active' : ''; ?>" data-target="#myCarousel" data-slide-to="<?php echo $counter2; ?>">
<a href="#">
<?php
the_post_thumbnail('thumbnail', array(
'class' => 'img-responsive'
));
?>
</a>
</div>
<?php endwhile; ?>
This will display it with your markup, but with a thumbnail image instead of the placeholder.

Resources