I'm currently developing a custom WordPress template. Within this template I'm trying to show all post from specific categories, see it as a sort of products segment (no selling or anything). So what I've got now is showing all posts image and title dynamically with all the styling and filtering through settings of ACF.
What I would like to achieve is the following result: (using bootstrap).
4 columns on each row, but when there are more than 4 posts. (so when there needs to be a second or third row of a specific category), Create a collapse functionality for showing posts 5 >.
So after some trying, I've come to the conclusion that the best way would be to create a for loop, in combination with filtering this would create the view I'm trying to create. Sadly after trying some different methods, I've got a bit stuck. The code is shown below:
<div id="items">
<?php
if (have_rows('products_category')) {
while (have_rows('products_category')) : the_row();
// Your loop code
$title = get_sub_field('product_category_name');
$slug = get_sub_field('product_category_slug');
/* Start the filter categpries segment */
$category = get_category_by_slug($slug);
$filter_id = $category->term_id;
$filters = array();
var_dump($filters);
array_push($filters, $filter_id);
var_dump($filters);
array_push($filters, 7);
var_dump($filters);
?>
<div id="items" class="row products margin-0 justify-content-between">
<div class=" <?php echo $filter_id ?> ">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 padding-0 padding-b-10">
<h2><?php echo $title ?></h2>
</div>
<div class="col-lg-12 padding-0">
<div id="products" class="row products margin-0 justify-content-between">
<?php $argsNew = array(
'offset' => 0,
'category' => $filters,
'orderby' => 'date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'author_name' => '',
'post_status' => 'publish',
'posts_per_page' => -1,
'suppress_filters' => false,
'connected_items' => get_queried_object(),
);
$posts_array = get_posts($argsNew);
$number_posts = count($posts_array);
echo $number_posts;
$i = 0;
foreach ($posts_array as $post) : setup_postdata($post);
$i++;
if($i <= 4) {
?>
<div class="col-lg-3 col-md-6 col-sm-6 col-xs-12 margin-fix padding-s-15 padding-b-30 image_filter">
<?php echo get_the_post_thumbnail($post->ID, '', array('class' => 'img-responsive big_image products_image img_small')); ?>
<?php the_title(sprintf('<h2 class="text-center big_image_product_text products_text"><a href="%s" rel="bookmark">', esc_url(get_permalink())),
'</a></h2>'); ?>
<?php
?>
</div>
<?php
}
else if($i > 4) {
?>
</div>
<div class="row">
<button data-toggle="collapse" class="btn-collapse" data-target="#products_collapse_<?php echo $filter_id ?>">Show more</button>
</div>
<div id="products_collapse_<?php echo $filter_id ?>" class="row products collapse margin-0 justify-content-between">
<div class="col-lg-3 col-md-6 col-sm-6 col-xs-12 margin-fix padding-s-15 padding-b-30 image_filter">
<?php echo get_the_post_thumbnail($post->ID, '', array('class' => 'img-responsive big_image products_image img_small')); ?>
<?php the_title(sprintf('<h2 class="text-center big_image_product_text products_text"><a href="%s" rel="bookmark">', esc_url(get_permalink())),
'</a></h2>'); ?>
</div>
</div>
<?php
}
endforeach;
wp_reset_postdata(); ?>
</div>
</div>
</div>
<?php
endwhile;
}
else {
// no rows found
echo "nothing found!";
}
?>
</div>
UPDATE
I've tried creating the for loop with the $number_posts and a count method but sadly then the view is badly corrupted. So my question is:
Create a for loop / counter to count the number of posts foreach category shown.
Specify the view on those results:
for -> items 1,2,3,4. Place them in a row. (1 1 1 1).
if(more than 4 items). Place items > 5 (and so on). Under a collapse block.
e.g.
3 items:
Normal view:
1 1 1.
7 items:
view + collapse (
1 1 1 1
-collapse button-
1 1 1
(more items)
)
Could anyone show me in the right direction and or help me with this one?
Thanks in advance!
PS: If you have any questions please ask them in the comments below
Assuming the field products_category returns an array of chosen category IDs:
This will work:
<div id="items">
<?php
// Retrieve all product categories
$terms = get_terms( 'product_cat' );
// Retrieve chosen categories to display
$specified_cats = get_field( "products_category" );
// Loop though product cats
foreach ( $terms as $term ) {
$filter_id = $term->term_id;
// If the current product category id is not in the array 'specified_cats' just to the next iteration
if(!in_array($filter_id, $specified_cats)){
continue;
}
$title = $term->name;
$slug = $term->slug;
?>
<div id="items" class="row products margin-0 justify-content-between">
<div class=" <?php echo $filter_id ?> ">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 padding-0 padding-b-10">
<h2><?php echo $title ?></h2>
</div>
<div class="col-lg-12 padding-0">
<div id="products" class="row products margin-0 justify-content-between">
<?php
$argsNew = array (
'offset' => 0,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'product_cat'=> $term->name
);
$posts_array = get_posts($argsNew);
$number_posts = count($posts_array);
$i = 0;
foreach ($posts_array as $post) : setup_postdata($post);
$i++;
if($i <= 4) {
?>
<div class="col-lg-3 col-md-6 col-sm-6 col-xs-12 margin-fix padding-s-15 padding-b-30 image_filter">
<?php echo get_the_post_thumbnail($post->ID, '', array('class' => 'img-responsive big_image products_image img_small')); ?>
<?php the_title(sprintf('<h2 class="text-center big_image_product_text products_text"><a href="%s" rel="bookmark">', esc_url(get_permalink())),
'</a></h2>'); ?>
<?php
?>
</div>
<?php
}else if($i > 4) {
?>
<div class="row">
<button data-toggle="collapse" class="btn-collapse" data-target="#products_collapse_<?php echo $filter_id ?>">Show more</button>
</div>
<div id="products_collapse_<?php echo $filter_id ?>" class="row products collapse margin-0 justify-content-between">
<div class="col-lg-3 col-md-6 col-sm-6 col-xs-12 margin-fix padding-s-15 padding-b-30 image_filter">
<?php echo get_the_post_thumbnail($post->ID, '', array('class' => 'img-responsive big_image products_image img_small')); ?>
<?php the_title(sprintf('<h2 class="text-center big_image_product_text products_text"><a href="%s" rel="bookmark">', esc_url(get_permalink())),
'</a></h2>'); ?>
</div>
</div>
<?php
}
endforeach;
wp_reset_postdata(); ?>
<div class="clearfix"></div>
</div>
<div class="clearfix"></div>
</div>
</div>
</div>
<?php
}
?>
</div>
The code is tested and working. I even included Bootstrap to make sure everything functioning correctly:
<!-- 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>
Related
I took over managing a WP site and do not have nearly the expertise that the previous person had. On the specific page, we have a magazine that shows related chess games that can be viewed that would fall under the magazine.
For some reason, the page will only display a maximum of 5 games at a time. I need to display as many games as there are that have the tag that matches the magazine. Below is the PHP code used for this page. Is anyone able to help me figure out what/where the issue lies?
<?php
/**
* Template Name: Antics Magazine Template
*
* #package WordPress
*/
get_header(); ?>
<main>
<?php if ( have_posts() ) :
while ( have_posts() ) : the_post();
?>
<section class="container py-5 text-center">
<h2 class="title--main text-uppercase mb-3">Antics <strong>Magazine</strong></h2>
<?php the_content(); ?>
<?php endwhile; endif; ?>
</section>
<section class="container pb-5">
<?php
$terms = get_terms( array(
'taxonomy' => 'magazine_type',
'hide_empty' => true,
'orderby' => 'id',
'order' => 'DESC'
) );
$i = 1;
//print_r($terms);
foreach($terms as $term){
//echo $term->term_id;
//echo $term->name;
//echo $term->slug;
if($i === 1 ){ $expand = 'true'; } else{ $expand = 'false'; }
?>
<div class="events">
<div class="events--header">
<?php echo $term->name; ?>
Read Magazine
<button class="btn ml-3" type="button" data-toggle="collapse" data-target="#collapsible-<?php echo $i; ?>" aria-expanded="<?php echo $expand; ?>">
<span class="sr-only">+</span>
</button>
</div>
<div class="collapse <?php if($i == 1){ echo 'show'; } ?>" id="collapsible-<?php echo $i; ?>">
<div class="events--body">
<?php
$args = array(
'post_type' => 'magazines',
'tax_query' => array(
array(
'taxonomy' => 'magazine_type',
'field' => 'slug',
'terms' => $term->slug,
),
),
);
$query = new WP_Query( $args );
if( $query->have_posts()): while( $query->have_posts()): $query->the_post();
?>
<div class="events--content">
<div class="row align-items-center">
<?php /*
<div class="col-xl-4">
<div class="events--date mb-xl-0">
<?php $date=date_create(get_the_date()); ?>
<span><?php echo date_format($date,"d"); ?></span> <?php echo date_format($date,"l, F j, Y"); ?>
</div>
</div>
*/ ?>
<div class="col-md">
<div class="events--title mb-0">
<?php the_title(); ?>
</div>
<!-- <p class="mb-md-0">Dothan Open</p> -->
</div>
<div class="col-md-auto">
View Game
<!-- Download Game -->
</div>
</div>
</div>
<?php endwhile; endif; ?>
</div>
</div>
</div>
<?php $i++; } ?>
</section>
</main>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="redacted" crossorigin="anonymous"></script>
<?php
get_footer();
I have tried messing with the loop counter thinking that perhaps the loop has a ceiling of 5, but that doesn't seem to be the case. I have verified that all six of the chess games are tagged appropriately/identically to one another.
Have you tried adding posts_per_page to the $args? Would look like this (more here):
$args = array(
'posts_per_page' => -1, // display all posts found
'post_type' => 'magazines',
'tax_query' => array(
array(
'taxonomy' => 'magazine_type',
'field' => 'slug',
'terms' => $term->slug,
),
),
);
<? get_header() ;?>
<?
$cat_param = 'select-category';
if (strpos($_SERVER['REQUEST_URI'], $cat_param)) {
$category_id = htmlspecialchars($_GET[$cat_param]);
} else {
$category_id = null;
}
?>
<article id="post-<? the_ID(); ?>" <? post_class('bg-white'); ?> data-file="<? echo basename(__FILE__); ?>">
<div class="wrap-outer">
<div class="py-5 py-md-6 py-lg-7 py-xl-8 wrap-inner">
<div class="container-fluid container-lg">
<div class="row">
<div class="col-12 col-md-8">
<form method="" action="<? echo get_post_type_archive_link('pcm_review'); ?>/">
<div class="form-group posts-filter">
<div class="input-group">
<div class="input-group-prepend">
<label class="input-group-text" for="review-select-category"><i class="fas fa-filter"></i></label>
</div>
<?
$args_cats = array(
'show_option_all' => 'Show All',
'orderby' => 'name',
'order' => 'ASC',
'show_count' => 0,
'echo' => 1,
'selected' => $category_id,
'name' => $cat_param,
'id' => $cat_param,
'class' => 'select-category form-control ml-auto',
'taxonomy' => 'category',
'hide_if_empty' => false,
'option_none_value' => -1,
'value_field' => 'term_id',
'required' => false,
);
wp_dropdown_categories( $args_cats );
?>
</div>
</div>
<script type="text/javascript">
jQuery(function() {
$('#select-category').change(function() {
this.form.submit();
});
});
</script>
<hr>
</form>
<?
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args_reviews = array(
'post_type' => 'pcm_review',
'cat' => $category_id,
'paged' => $paged,
);
$query_reviews = new WP_Query( $args_reviews );
if ( $query_reviews->have_posts() ) {
while ( $query_reviews->have_posts() ) {
$query_reviews->the_post();
$content = get_post_meta(get_the_ID(), 'pcm_review_content', true);
$name = get_post_meta(get_the_ID(), 'pcm_review_name', true);
$rating_str = get_post_meta(get_the_ID(), 'pcm_review_rating', true);
$rating_int = intval($rating_str);
$rating_star = '<i class="fas fa-star mr-1 text-warning"></i>';
$rating_stars = str_repeat($rating_star, $rating_int);
?>
<div id="post-<? the_ID(); ?>" <? post_class(''); ?>>
<p>
<span class="review-stars"><? echo $rating_stars; ?></span><br>
<b><? the_title(); ?></b>
</p>
<div class="review-content">
<? echo $content; ?>
</div>
<div class="justify-content-between row small text-muted">
<div class="col-auto">
<i class="fas fa-user mr-2"></i><span class="review-name"><? echo $name; ?></span>
</div>
<div class="col-auto">
<i class="fas fa-tag mr-2"></i><span class="review-categories"><? echo get_the_category_list(', '); ?></span>
</div>
</div>
<hr>
</div>
<?
}
the_posts_pagination();
} else {
pcm_no_results();
}
wp_reset_postdata();
?>
</div>
<div class="col-12 col-md-4">
<div class="sidebar-wrap sticky-top">
<? get_template_part('template-parts/aside-blog'); ?>
<? get_template_part('template-parts/aside-reviews'); ?>
</div>
</div>
</div>
</div>
</div>
</div>
</article>
<? get_footer() ;?>
Struggling with a pagination issue and would really appreciate any help. I’m not making any progress in searching here or other forums so thought I’d ask for help.
Background
I have a custom post type for testimonials/reviews which supports the category taxonomy. My template file includes a dropdown/select-list to allow the visitor to choose a category if they’d like to filter the reviews and only see reviews for their selected category.
Problem
Okay so let’s say I have 10 total reviews and only 5 of them are assigned to ‘Category A’. If the visitor filters the posts by ‘Category A’, they will still see pagination links as if there are 10 posts instead of the 5 that belong to ‘Category A’. I hope that makes sense.
No errors in console or debug log.
Code Explained
Line 5 starts a little snippet just to make things easier later in th file. Setting a var to the value of a category ID if the category parameter is found in the URL. If not, we set the category ID to null.
Line 20 start the form that contains the wp_dropdown_categories() function which outputs a list of my categories. We set the selected param to the same value as the category ID from line 5. The form action attribute is set to the main archive url for the reviews so that if a category is selected and the user is on any page other than the first, they’re pointed back to the root review archive url.
Line 47 just forces the form to submit when an option from the dropdown is selected.
Line 57 is just telling WordPress which page we’re currently on, then the rest of the query.
By adding the 'total'=> $query_reviews->max_num_pages parameter to my pagination function, this gets outputs the correct number of pagination links since it uses the value of max_num_pages of my query rather than the main query. Crisis averted.
I have been searching for a solution to this issue for over a week and I haven't been able to find anybody else with the same trouble.
I am working on a custom WP theme that somebody else built. There is a single-page template that I need to implement paging on for one of the secondary loops. I have been attempting to use the built-in paginate_links() function, as well as other methods. The pagination links show up, but when I click on a pagination link it doesn't go to that page in the pagination. Instead the original page is reloaded (i.e. instead of going to thewebsite.com/my-page/page/2/, it reloads thewebsite.com/my-page/).
The previous dev used this filter in functions.php to load the correct template:
add_filter('single_template', create_function('$t', 'foreach( (array) get_the_category() as $cat ) { if ( file_exists(TEMPLATEPATH . "/single-{$cat->slug}.php") ) return TEMPLATEPATH . "/single-{$cat->slug}.php"; } return $t;' ));
And here is my template file:
<?php
/**
* Template Name: Project Template
*/
get_header('news'); ?>
<article role="main" class="projectpage">
<div class="container">
<section class="pagecontent">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<section class="overview">
<h1><?php the_title(); ?></h1>
<div>
<?php the_content(); ?>
</div>
<div>
<?php if(get_post_meta($post->ID, 'pagelink', true)): ?>
Read the Overview
<?php endif; ?>
</div>
</div><!--end row-->
</section><!--end overview-->
<?php endwhile ?>
<?php wp_reset_postdata() ?>
<? endif ?>
<section class="related">
<div>
<h1> Related Resources </h1>
<h2> Explore our library of articles and resources </h2>
</div>
<div class="row">
<div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 relatedlinks">
<section class="projectcategories">
<h3> Categories </h3>
<ul>
<?php wp_list_categories( array(
'orderby' => 'id',
'show_count' => true,
'use_desc_for_title' => false,
'child_of' => 93,
'title_li' => ' '
) ); ?>
</ul>
</section>
<section class="project-search" role="search">
<form method="get" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<input type="hidden" name="cat" id="cat" value="93" />
<input type="text" size="16" name="s" placeholder="search keywords" class="search-box" />
<input type="submit" value="Go" class="go"/>
</form>
</section>
<section class="otherprojects">
<h3> Other Projects </h3>
<?php
$args = array(
'category__in' => 91,
'post__not_in' => array( $post->ID )
);
// the query
$query = new WP_Query( $args );
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $query;
// The Loop
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
<?php the_title(); ?>
<? endwhile ;
/* Restore original Post Data */
wp_reset_postdata();
endif;
$wp_query = NULL;
$wp_query = $temp_query;
?>
</section>
</div><!--end col 1-->
<div class="col-lg-9 col-md-9 col-sm-9 col-xs-9">
<section class="articles">
<?php
// THIS IS THE SECTION WHERE I NEED THE PAGINATION
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = [
'posts_per_page' => 3,
'paged' => $paged,
'post_type' => 'post',
'order' => 'DESC',
'post__not_in' => array( $post->ID ),
'tax_query' => [
[
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => '93',
],
],
];
$custom_query = new WP_Query( $args );
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $custom_query;
if ( $custom_query->have_posts() ) {
while ( $custom_query->have_posts() ) {
$custom_query->the_post(); ?>
<div class="row">
<div class="col-md-2 col-sm-2 col-xs-2 divider">
<p class="date"><?php the_time('M j') ?></p>
</div><!--end col-->
<div class="col-md-4 col-sm-4 col-xs-4">
<div class="articleimg">
<?php if ( has_post_thumbnail()) {?>
<?php the_post_thumbnail('blog-thumb'); ?>
<?php } ?>
</div><!--end blogimg-->
</div><!--end col-->
<div class="col-md-6 col-sm-6 col-xs-6">
<div class="blogcontent">
<h3><?php the_title();?></h3>
<p><?php the_excerpt(); ?></p>
// read more
</div><!--end blogcontent-->
</div><!--end col-->
</div><!--end row-->
<?php }
}
echo paginate_links(array(
'total' => $wp_query->max_num_pages
));
$wp_query = NULL;
$wp_query = $temp_query;
wp_reset_postdata(); ?>
</section><!--end articles-->
</div><!--end col 2-->
</div> <!--end row-->
</section><!--end related-->
<!-- ANNOUNCEMENTS -->
<!--ANNOUNCEMENT SECTION -->
<!-- dynamic content --filters posts by category and only shows 'member' posts with a limit of six posts being
displayed-->
<section id="announcement-front" class="clearfix">
<div class="container">
<div>
<?php $query = new WP_Query('posts_per_page=1&category_name=advertisement');
if ($query->have_posts()) :
while ($query->have_posts()) : $query->the_post(); ?>
<a href="<?php the_permalink()?>" <?php the_content();?> </a>
<?php endwhile ?>
<? endif ?>
<?php wp_reset_postdata() ?>
</div><!--end row-->
</div><!--container-->
</section><!--end announcement-->
</section> <!--end page content -->
</div><!--end container-->
</article>
<?php get_footer(); ?>
I realize there is a whole galaxy of WordPress pagination tutorials and threads out there, but I haven't been able to find one yet that solves this particular problem.
I think your code should mostly work. You don't need to worry about all the saving and switching of $wp_query though - have you tried this simpler snippet? It worked ok in my environment (though I had to modify the query args slightly to get it to return any results).
If that's not working can you post the HTML it generates?
<section class="articles">
<?php
// THIS IS THE SECTION WHERE I NEED THE PAGINATION
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = [
'posts_per_page' => 3,
'paged' => $paged,
'post_type' => 'post',
'order' => 'DESC',
'post__not_in' => array( $post->ID ),
'tax_query' => [
[
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => '93',
],
],
];
$custom_query = new WP_Query( $args );
if ( $custom_query->have_posts() ) {
while ( $custom_query->have_posts() ) {
$custom_query->the_post(); ?>
<div class="row">
<div class="col-md-2 col-sm-2 col-xs-2 divider">
<p class="date"><?php the_time('M j') ?></p>
</div><!--end col-->
<div class="col-md-4 col-sm-4 col-xs-4">
<div class="articleimg">
<?php if ( has_post_thumbnail()) {?>
<?php the_post_thumbnail('blog-thumb'); ?>
<?php } ?>
</div><!--end blogimg-->
</div><!--end col-->
<div class="col-md-6 col-sm-6 col-xs-6">
<div class="blogcontent">
<h3><?php the_title();?></h3>
<p><?php the_excerpt(); ?></p>
// read more
</div><!--end blogcontent-->
</div><!--end col-->
</div><!--end row-->
<?php }
}
echo paginate_links(array(
'total' => $custom_query->max_num_pages
));
wp_reset_postdata(); ?>
</section><!--end articles-->
</div><!--end col 2-->
</div> <!--end row-->
</section><!--end related-->
Thanks, Jo! That article pointed me in the right direction. I checked in dev tools and I was indeed getting a 301. I tried the code snippet from the article you pointed me to, and it didn't quite work, so I googled "fix pagination with redirect_canonical" and this was the first article that popped up. I took the function from there, threw it in to functions.php, et voilà! It's the same method, but, I think, not passing a custom post type into the conditional. I hope this can help someone in the future. It was a real pain. It was also a reminder of how very little I really know about WP and how much I want to learn about it! Thanks again.
Here's the code:
add_filter('redirect_canonical','custom_disable_redirect_canonical');
function custom_disable_redirect_canonical($redirect_url) {
if (is_paged() && is_singular()) $redirect_url = false;
return $redirect_url;
}
I am working in a custom WP theme.I need to show each posts under individual categories, which is working fine.
I changed the category to taxonomy.Now, i want to show more info under each category name,but, i cannot understand,where should i put my code in the loop.
Specially the post counts under each categories.
<?php
/*
Template Name: Home Page
*/
get_header();
global $redux_demo;
?>
<div class="sroll"><div class="container">
<marquee><p> <?php echo $redux_demo['main-option-marquee']; ?></p></marquee>
</div></div>
<div class="container">
<div class="row">
<div class="col-sm-9"> </br>
<div class="content mCustomScrollbar" style="height: 690px;">
<?php
$terms = get_terms( array(
'taxonomy' => 'category',
'hide_empty' => false,
) );
foreach($terms as $cat){
$cata_name = $cat->name;
$term_id = $cat->term_id;
?>
<div class="col-sm-6 col-md-4 col-lg-3 p10">
<div class="box">
<?php
//echo '<h3>'.$catname[0]->cat_name.'</h3>';
?><h3><a href="<?php echo home_url('index.php/category/'.$cata_name) ?>">
<?php echo $cata_name; ?></a></h3> <?php
$catqueryy = new WP_Query( 'cat='.$term_id.'&posts_per_page=4' );
while($catqueryy->have_posts()) : $catqueryy->the_post();
?>
<p class="post_title"><?php echo ''.__(get_the_title(),'rockon').''; ?></p>
<p class="post_cont"><?php echo get_the_excerpt(); ?></p>
<?php
endwhile;
?>
</div>
</div>
<?php } ?>
</div></br>
</div>
<div class="col-sm-3">
<h1></h1>
<?php get_sidebar(); ?>
<h1></h1>
</div>
</div>
</div>
<?php
get_footer();
?>
Custom taxonomy try:
$the_query = new WP_Query( array(
'post_type' => 'CUSTOM_POST_TYPE',
'tax_query' => array(
array(
'taxonomy' => 'CUSTOM_TAXONOMY',
'field' => 'id',
'terms' => TERM_ID
)
)
) );
$count = $the_query->found_posts;
https://wordpress.org/support/topic/counting-posts-within-categories
For Example:
<?php
/*
Template Name: Home Page
*/
get_header();
global $redux_demo;
?>
<div class="sroll"><div class="container">
<marquee><p> <?php echo $redux_demo['main-option-marquee']; ?></p></marquee>
</div></div>
<div class="container">
<div class="row">
<div class="col-sm-9"> </br>
<div class="content mCustomScrollbar" style="height: 690px;">
<?php
$terms = get_terms( array(
'taxonomy' => 'category',
'hide_empty' => false,
) );
foreach($terms as $cat){
$cata_name = $cat->name;
$term_id = $cat->term_id;
?>
<div class="col-sm-6 col-md-4 col-lg-3 p10">
<div class="box">
<?php
//echo '<h3>'.$catname[0]->cat_name.'</h3>';
?><h3><a href="<?php echo home_url('index.php/category/'.$cata_name) ?>">
<?php echo $cata_name; ?></a></h3>
<?php
$catqueryy = new WP_Query( 'cat='.$term_id.'&posts_per_page=4' );
$count = $catqueryy->found_posts;
?>
<h3><?php echo "Post Count : ".$count; ?></h3>
<?php
while($catqueryy->have_posts()) : $catqueryy->the_post();
?>
<p class="post_title"><?php echo ''.__(get_the_title(),'rockon').''; ?></p>
<p class="post_cont"><?php echo get_the_excerpt(); ?></p>
<?php
endwhile;
?>
</div>
</div>
<?php } ?>
</div></br>
</div>
<div class="col-sm-3">
<h1></h1>
<?php get_sidebar(); ?>
<h1></h1>
</div>
</div>
</div>
<?php
get_footer();
?>
You can check this Link enter link description here
if it helps.. I guess, some one will write this code here as I also need to understand how it will work after you pass a query.
Actually you do not need to count anything by manually written code. If you look at the get_terms() description, you can see that WP counts this for you (if you set 'pad_counts' to true (or 1)). With this turned on there will be a "count" key and a number value in your response array with each category.
You can just "echo" it where you want to.
This way your
$terms = get_terms( array(
'taxonomy' => 'category',
'hide_empty' => false,
) );
should look like this:
$terms = get_terms( array(
'taxonomy' => 'category',
'hide_empty' => false,
'pad_counts' => true,
) );
Notice that I added 'pad_counts' => true, to the query, so you will have the number you are looking for - without writing too much code.
If you want to do it manually, I suggest that you create a loop that fills an array with 'category => number' elements, and look up the required key-value pairs in the loop that writes out the HTML.
I was also searching for something similar to this question. I think, you need a code to display the post counts of each categories.
I'm trying to place an element (corner ribbon) on certain term images in my archive. I'm using Advanced Custom Fields to assign a value to certain terms. The code below however places a ribbon on EVERY term and not just the one's with the "active15" value associated. Can someone help me with what I'm doing wrong?
This LINK shows a sample of what is happening. Aluminum is the only image that should have the ribbon, but for some reason it seems like the filter isn't being applied.
<!-- Green Ribbon for Top 15 Groups Start -->
<?php
if($terms) {
foreach($terms as $lc) {
if( get_field('group_active_in_focus_15', 'focus15groups_'.$lc->term_id) != 'active15' ) continue;
{
?>
<div class="ribbon ribbon-green">
<div class="banner">
<div class="text">TOP 15</div>
</div>
</div>
<?php } ?>
<?php
}
}
else
{
echo '<div class="ribbon ribbon-blue">
<div class="banner">
<div class="text">TOP 15</div>
</div>
</div>';
}
?>
<!-- Green Ribbon for Top 15 Groups End -->
CODE TO GET/DISPLAY TERMS FOR PAGE
<div class="row">
<?php
if ( get_query_var( 'paged' ) ) {
$paged = get_query_var('paged');
}elseif( get_query_var( 'page' ) ) {
$paged = get_query_var( 'page' );
}else{
$paged = 1;
}
$per_page = 12;
#fix
$term_args = array(
'hide_empty' => 0,
'exclude' => array(16, 20, 22, 25, 27, 28, 30, 4, 33, 264 ), //* Enter ID's of parent categories to exclude from list
);
$number_of_terms = wp_count_terms( 'focus15groups' , $term_args); // This counts the total number terms in the taxonomy with a function)
$paged_offset = ($paged - 1) * $per_page;
$libargs = array(
'orderby' => 'name',
'order' => 'ASC',
'exclude' => array(16, 20, 22, 25, 27, 28, 30, 4, 33, 264 ), //* Enter ID's of parent categories to exclude from list
'number' => $per_page,
'offset' => $paged_offset,
);
$_libargs = wp_parse_args($term_args, $libargs);
$libcats = get_terms( 'focus15groups', $_libargs);
#fix
$i = 0;
foreach($libcats as $lc){
if( $i % 4 == 0 ) { ?>
<div class="clearfix"></div>
<?php }
$i++; ?>
<div class="col-lg-3">
<?php $termlink = get_term_link( $lc->slug, 'focus15groups' ); ?>
<div class="panel panel-default <?php the_field('group_active_in_focus_15', 'focus15groups_'.$lc->term_id); ?>">
<div class="panel-image">
<!-- Green Ribbon for Top 15 Groups Start -->
<?php
if($terms) {
foreach($terms as $lc) {
if( get_field('group_active_in_focus_15', 'focus15groups_'.$lc->term_id) != 'active15' ) continue;
{
?>
<div class="ribbon ribbon-green">
<div class="banner">
<div class="text">TOP 15</div>
</div>
</div>
<?php } ?>
<?php
}
}
else
{
echo '<div class="ribbon ribbon-blue">
<div class="banner">
<div class="text">TOP 15</div>
</div>
</div>';
}
?>
<!-- Green Ribbon for Top 15 Groups End -->
<?php if( get_field('group_active_in_focus_15', 'focus15groups_'.$term->term_id) != 'active15' ) { ?>
<div class="ribbon ribbon-green">
<div class="banner">
<div class="text">TOP 15</div>
</div>
</div>
<?php } else { ?>
<?php } ?>
<div class="thumbnail">
<div class="caption">
<br/><br/>
<h1><span class="label label-info"><?php echo $lc->count ?></span></h1>
<p> Symbols </p>
<h4> <a class="label label-default" href="<?php echo $termlink; ?>"> View Group</a> </h4>
</div>
<!-- Get Image by Attachment ID Start-->
<?php
$attachment_id = get_field('taximage', 'focus15groups_'.$lc->term_id);
if ($attachment_id) {
$image = wp_get_attachment_image_src($attachment_id, 'industrygroup-img');
if ($image) {
?>
<img class="img-responsive" src="<?php echo $image[0]; ?>" />
<?php
}
}
else { ?>
<img class="img-responsive" src="http://www.runningalpha.com/wp-content/uploads/2014/08/RA-logo-300px-groups.jpg" alt="<?php the_title(); ?>" />
<?php } ?>
</div>
<!-- Get Image by Attachment ID End-->
</div>
<div class="panel-footer text-center"><?php echo $lc->name; ?>
</div>
</div>
</div>
<?php } ?>
</div>
I managed to find a fix using the following. By moving my code to the else statement made it show for the correct terms. Something seems odd about that, but it works right!
<?php if( get_field('group_active_in_focus_15', 'focus15groups_'.$lc->term_id) != 'active15' ) { ?>
<?php } else { ?>
<div class="ribbon ribbon-green">
<div class="banner">
<div class="text">TOP 15</div>
</div>
</div>
<?php } ?>