Show parent child taxonomy in nested tabs with its post - wordpress

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.

Related

wordpress primary and secondary navigation

The below is my landing page "Products". it will display 2 columns, in the left is the secondary navigation and the right is the content. i want that if someone click in the primary navigation "Products". In second navigation the first item will in bold and will display the content.
<?php
/*
Template Name: Product Navigation Page
*/
?>
<?php get_header(); ?>
<div class="container page-border-top">
<div id="product-navigation" class="col-md-3" style="border: 1px solid green;">
<div id="navbar" class="navbar-collapse collapse">
<?php
$args2 = array(
'menu' => 'product-menu',
'menu_class' => 'nav navbar-nav',
'theme_location' => 'product-menu',
'container' => 'false'
);
wp_nav_menu( $args2 );
?>
</div><!--/.navbar-collapse -->
</div>
<div class="col-md-9" style="border: 1px solid red;">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
</div>
</div>
<?php get_footer(); ?>
If your landing page "Products" has no parent, and if all products child pages of "Products", then you can use this template file for the landing page and for all child pages (products):
<?php
/*
Template Name: Product Navigation Page
*/
$redirect = false;
$parent_id = wp_get_post_parent_id( $post->ID );
if ( ! $parent_id ) {
$redirect = true;
$parent_id = $post->ID;
}
// get all child's
$all_wp_pages = $wp_query->query(array(
'post_type' => 'page',
'order' => 'ASC',
'orderby' => 'menu_order',
'post_status' => 'publish'
));
$childs = get_page_children( $parent_id, $all_wp_pages );
if ( $redirect ) {
wp_redirect( get_permalink( $childs[0]->ID ) );
exit;
}
$current_page_id = $post->ID;
?>
<?php get_header(); ?>
<div class="container page-border-top">
<div id="product-navigation" class="col-md-3" style="border: 1px solid green;">
<div id="navbar" class="navbar-collapse collapse">
<ul class="menu">
<?php foreach ( $childs as $child ): ?>
<li class="<?php if ( $current_page_id == $child->ID ): ?>current-menu-item<?php endif; ?>"><?php echo $child->post_name; ?></li>
<?php endforeach; ?>
</ul>
</div><!--/.navbar-collapse -->
</div>
<div class="col-md-9" style="border: 1px solid red;">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
</div>
</div>
<?php get_footer(); ?>
You dont't have to create a menu for the products, it just list all child pages, and you can sort the products by the Order field (Page Attributes).
Step by Step:
Create a page "Products" and select the template file above for it (Product Navigation Page)
Create some product pages as childs of "Products" and select also the template file above for it (Product Navigation Page).
The template file will automaticly create the desired secondary navigation on the left side. The current product has a css class current-menu-item, so you can style it in bold.
This part generates the second nav:
<ul class="menu">
<?php foreach ( $childs as $child ): ?>
<li class="<?php if ( $current_page_id == $child->ID ): ?>current-menu-item<?php endif; ?>"><?php echo $child->post_name; ?></li>
<?php endforeach; ?>
</ul>

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 display the category wise news in tabs ?? I am using news manager plugin for adding news?

Till now i have listed the category in tabs. But did not get the news and its description under the category. And in database the news are treated as post's.
And I am using the jquery tabs for displaying news..
MY CODE:---
<?php
/*
Template Name: tabbing
*/
?>
<?php get_header(); ?>
<div id="tab">
<?php
//list terms in a given taxonomy (useful as a widget for twentyten)
$taxonomy = 'news-category';
$tax_terms = get_terms($taxonomy);
?>
<ul class="nav nav-tabs" role="tablist">
<?php $counter = 0; foreach ($tax_terms as $tax_term) { ?>
<li role="presentation" class="post-<?php ?> <?=($counter == 1) ? 'active' : ''?>"><?php echo $tax_term->name; ?></li>
<?php $counter++; } //exit; ?>
</ul>
/* Right all description is displaying but not according to the category... */
<div class="tab-content">
<?php
$counter = 0;
$loop = new WP_Query( array( 'post_type' => 'news', 'posts_per_page' => -1 ) );
while ( $loop->have_posts() ) : $loop->the_post();
$counter++;
?>
<div role="tabpanel" class="tab-pane <?=($counter == 1) ? 'active' : ''?>" id="post-<?php the_ID(); ?>"><?php the_content();?></div>
<?php endwhile; ?>
</div>
</div>
<?php get_footer(); ?>

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.

Display "NEW" image based on timestamp of post in Wordpress

I want to display an image "NEW" next to my post if:
It was published between 1 to 45 days ago.
Hide/disable the "NEW" image after 45 days.
Here's my code for my carousel which displays the latest 4 posts:
`<!------Begin Carousel Cusomizations-------->
<div class="list_carousel">
<a id="prev2" class="prev" href="#"><</a>
<ul id="foo2">
<?php
global $post;
$args = array( 'numberposts' => -1, 'post_type' => 'guides');
$posts = get_posts($args);
foreach( $posts as $post ) : setup_postdata($post); ?>
<li class="productCarousel" onclick="document.location.href='<?php echo the_permalink(); ?>';">
<div class="liContent">
<div class="ribbon"><div class="ribbon-new">New</div></div>
<div class="liPadding">
<?php the_post_thumbnail( array(75,75) ); ?>
<h4><?php the_title(); ?></h4>
<p><?php the_excerpt(); ?></p>
<p class="accessGuides">Access Guide</p>
</div>
</div>
</li>
<?php endforeach; ?>
</ul>
<a id="next2" class="next" href="#">></a>
<div class="clearfix"></div>
<div id="pager2" class="pager"></div>
</div>
</div>
<!----End Carousel Customizations----->`
The DIV section: <div class="ribbon"><div class="ribbon-new">New</div></div> is the container for my "NEW" image banner.
Any assistance is greatly appreciated!
<?php
// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
// posts in the last 45 days
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-45 days')) . "'";
return $where;
}
?>
<!------Begin Carousel Cusomizations-------->
<div class="list_carousel">
<a id="prev2" class="prev" href="#"><</a>
<ul id="foo2">
<?php
global $post;
$args = array( 'numberposts' => -1, 'post_type' => 'guides');
add_filter( 'posts_where', 'filter_where' );
$posts = get_posts($args);
foreach( $posts as $post ) : setup_postdata($post); ?>
<li class="productCarousel" onclick="document.location.href='<?php echo the_permalink(); ?>';">
<div class="liContent">
<div class="ribbon"><div class="ribbon-new">New</div></div>
<div class="liPadding">
<?php the_post_thumbnail( array(75,75) ); ?>
<h4><?php the_title(); ?></h4>
<p><?php the_excerpt(); ?></p>
<p class="accessGuides">Access Guide</p>
</div>
</div>
</li>
<?php endforeach;
remove_filter( 'posts_where', 'filter_where' );
?>
</ul>
<a id="next2" class="next" href="#">></a>
<div class="clearfix"></div>
<div id="pager2" class="pager"></div>
</div>
<!----End Carousel Customizations----->`
Updated answer to display the text "New Ribbon" when the post is published in last 45 days
<!------Begin Carousel Cusomizations-------->
<div class="list_carousel">
<a id="prev2" class="prev" href="#"><</a>
<ul id="foo2">
<?php
global $post;
$args = array( 'numberposts' => -1, 'post_type' => 'guides');
$posts = get_posts($args);
foreach( $posts as $post ) : setup_postdata($post); ?>
<li class="productCarousel" onclick="document.location.href='<?php echo the_permalink(); ?>';">
<div class="liContent">
<?php if( strtotime('-45 days') < strtotime( $post->post_date ) ) {
<div class="ribbon">
<div class="ribbon-new">New</div>
</div>
<?php } ?>
<div class="liPadding">
<?php the_post_thumbnail( array(75,75) ); ?>
<h4><?php the_title(); ?></h4>
<p><?php the_excerpt(); ?></p>
<p class="accessGuides">Access Guide</p>
</div>
</div>
</li>
<?php endforeach; ?>
</ul>
<a id="next2" class="next" href="#">></a>
<div class="clearfix"></div>
<div id="pager2" class="pager"></div>
</div>
<!----End Carousel Customizations----->

Resources