Last active
January 1, 2018 06:30
-
-
Save dasbairagya/068a657e888bc7e2d6b44f6ca3c20991 to your computer and use it in GitHub Desktop.
category page for custom post type(archive.php)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| get_header(); | |
| global $post; | |
| global $wpdb; | |
| $term = get_queried_object();//get the current term | |
| $taxonomy = $term->taxonomy; | |
| $term_id = $term->term_id;//get the currnet term ID | |
| $term_name = $term->name;//get the current term name | |
| $parent_id = empty( $term_id ) ? 0 : $term_id;//get the parent id of the currnet term | |
| $term_parent = get_term($term->parent);//get the parent term from the child term | |
| $parent_term_link = get_term_link($term_parent);//get the parent term link | |
| $product_categories = get_categories(array( | |
| 'parent' => $parent_id, | |
| //'menu_order' => 'ASC', | |
| 'hide_empty' => 0, | |
| 'hierarchical' => 1, | |
| 'taxonomy' => 'blog_cat', | |
| 'pad_counts' => 1, | |
| 'order' => 'DESC', | |
| 'orderby' => 'name' | |
| ) | |
| ); | |
| $product_categories = wp_list_filter( $product_categories );//get the sub categories of a parent category | |
| ?> | |
| <div class="inner-page-header"> | |
| <div class="container"> | |
| <div class="row"> | |
| <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12"> | |
| <div class="header-page-title"> | |
| <h2><?php echo ($taxonomy=='blog_cat')?'Category: '.$term_name:'Tag: '.$term_name;?></h2> | |
| </div> | |
| </div> | |
| <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12"> | |
| <div class="header-page-locator"> | |
| <ul> | |
| <li><a href="<?php echo esc_url( home_url( '/' ) ); ?>">Home/</a></li> | |
| <li><a href="<?php echo site_url();?>/my-blog">Blog/</a></li> | |
| <?php if($term_parent->name!=null){ ?> | |
| <li class="active text-theme-colored2"><a href="<?php echo esc_url( $parent_term_link ); ?>"><?php echo $term_parent->name; ?></a></li> | |
| <?php } ?> | |
| <li><?php echo $term->name; ?></li> | |
| </ul> | |
| </div> | |
| </div> | |
| </div> | |
| </div> | |
| </div> | |
| <div class="news-page-area"> | |
| <div class="container"> | |
| <div class="row"> | |
| <!-- if there is parent category exits --> | |
| <?php if($product_categories){ ?> | |
| <div class="col-lg-9 col-md-9 col-sm-9 col-xs-12"> | |
| <div class="news-main-content"> | |
| <?php foreach ($product_categories as $category) { | |
| $term_link = get_term_link($category->term_id, 'blog_cat'); | |
| $image = get_field('image', $category); | |
| $image = empty($image) ? 'https://wp.goigi.me/graykon/wp-content/uploads/2017/12/noimage.png':$image; | |
| ?> | |
| <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12"> | |
| <div class="single-news-area"> | |
| <div class="news-featured-image"> | |
| <a href="<?php the_permalink();?>"><img src="<?php echo $image;?>" alt="news"></a> | |
| <div class="date-area"> | |
| <ul> | |
| <li><?php echo get_the_date();?></li> | |
| </ul> | |
| </div> | |
| </div> | |
| <h3><a href="<?php the_permalink();?>"><?php the_title();?></a></h3> | |
| <ul> | |
| <li><span>by</span> <?php echo get_the_author();?> |</li> | |
| <li><span></span> Comments</li> | |
| </ul> | |
| </div> | |
| </div> | |
| <?php } /*end foreach*/ ?> | |
| </div> | |
| </div> | |
| <!--end if there is parent category exits --> | |
| <?php } else { | |
| if (get_query_var('paged')) { | |
| $paged = get_query_var('paged'); | |
| } elseif (get_query_var('page')) { | |
| $paged = get_query_var('page'); | |
| } else { | |
| $paged = 1; | |
| } | |
| $args = array( | |
| 'post_type' => 'blog', | |
| 'tax_query' => array( | |
| array( | |
| 'taxonomy' => $term->taxonomy, | |
| 'field' => 'id', | |
| 'terms' => $term_id | |
| ) | |
| ), | |
| 'numberposts' => -1, | |
| 'posts_per_page' => 2, | |
| 'post_status' => 'publish', | |
| 'post_parent' => null, | |
| 'paged' => $paged, | |
| 'order' => 'DESC' | |
| ); | |
| $custom_query = new WP_Query($args); | |
| //var_dump($custom_query); | |
| if ($custom_query->have_posts()) : | |
| ?> | |
| <div class="col-lg-9 col-md-9 col-sm-9 col-xs-12"> | |
| <div class="news-main-content"> | |
| <?php | |
| while ($custom_query->have_posts()) : $custom_query->the_post(); | |
| $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); | |
| $product_image = empty($url) ? 'https://wp.goigi.me/graykon/wp-content/uploads/2017/12/noimage.png':$url; | |
| ?> | |
| <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12"> | |
| <div class="single-news-area"> | |
| <div class="news-featured-image"> | |
| <a href="<?php the_permalink();?>"><img src="<?php echo $url;?>" alt="news"></a> | |
| <div class="date-area"> | |
| <ul> | |
| <li><?php echo get_the_date();?></li> | |
| <!-- <li>26</li> | |
| <li>June</li> --> | |
| </ul> | |
| </div> | |
| </div> | |
| <h3><a href="<?php the_permalink();?>"><?php the_title();?></a></h3> | |
| <ul> | |
| <li><span>by</span> <?php echo get_the_author();?> |</li> | |
| <li><span></span> Comments</li> | |
| </ul> | |
| <p><?php echo substr(get_the_excerpt(), 0, 180); ?></p> | |
| </div> | |
| </div> | |
| <?php endwhile; ?> | |
| <div class="row"> | |
| <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 text-center"> | |
| <?php if ($custom_query->max_num_pages > 1) : | |
| $orig_query = $wp_query; | |
| $wp_query = $custom_query; | |
| ?> | |
| <?php | |
| if (function_exists('wp_bootstrap_pagination')){ | |
| wp_bootstrap_pagination(); | |
| } | |
| ?> | |
| <?php $wp_query = $orig_query; ?> | |
| <?php endif; ?> | |
| <?php wp_reset_postdata();?> | |
| </div> | |
| </div> | |
| </div> | |
| </div> | |
| <?php endif; ?> | |
| <?php } ?> | |
| <div class="col-lg-3 col-md-3 col-sm-3 col-xs-12"> | |
| <?php echo get_sidebar();?> | |
| </div> | |
| </div> | |
| </div> | |
| </div> | |
| <?php get_footer();?> |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is the sidebar.php code....
Categories
'blog_cat','hide_empty' => false,) ); foreach ($cat_terms as $cat_term){ echo '