Is there a way to get the current taxonomy (category or tag) post count? The code i'm having is only for category
<?php
$cat = get_query_var( 'cat' );
$categories = get_categories( 'include='.$cat );
if ( $categories ) {
foreach( $categories as $category ) {
echo '' . $category->count;
}
}
?>
I guess you can do something like this:
// Set the name of your Taxonomy or get it as you're currently doing
// It can be category, tag or custom taxonomy name
$taxonomy = "your_taxonomy";
$total_count = 0;
// Get all the terms in your Taxonomy and add the count for each term
foreach ( get_terms( $taxonomy ) as $term ) {
$total_count += (int) $term->count;
}
echo $total_count;
This will give you the count of all the posts that have assigned ANY term in the Taxonomy your_taxonomy, which I understand is what you want...
Related
I want to customize WooCommerce category page (archive-product.php) to show current product category, but also I want to create Previous & Next links to previous and next category term links somewhere in the page.
I've tried a lot of solutions with no luck. I've manage to implement a code that take actual category ID, I've added +1 and -1 and I was able to create the links but the big issue is when 'next link' is on the last category that is not working, is not infinite loop for the links. I.E if I'm on cat page 6 that is last category I have, next one should be 1 and prev one should be 5 and if I'm on first category, prev one should be 6 and next one should be 2.
Please find below the piece of code I`m using and is working but not as I need:
<?php
$taxonomy = 'product_cat';
$cate = get_queried_object();
$cateID = $cate->term_id;
$next_cateID = $cateID + 1;
$prev_cateID = $cateID - 1;
$next_term_link = get_term_link( $next_cateID, $taxonomy );
$next_term = get_term( $next_cateID, $taxonomy );
$next_slug = $next_term->name;
$prev_term_link = get_term_link( $prev_cateID, $taxonomy );
$prev_term = get_term( $prev_cateID, $taxonomy );
$prev_slug = $prev_term->name;
?>
<p><?php echo $prev_slug; ?></p>
<p><?php echo $next_slug; ?></p>
Updated:
You just need to define your first and your last categories Ids. Then it will work as an infinite loop (assuming that your product category terms are sequential):
<?php
if ( is_product_category() ) :
$taxonomy = 'product_cat'; // WooCommerce product category taxonomy
$term_id = get_queried_object_id(); // Current product category term Id
// Get first product category term Id
$query_args = array('taxonomy' => 'product_cat', 'hide_empty' => false, 'orderby' => 'term_id', 'number' => '1', 'fields' => 'ids');
$term_id_1st = get_terms($query_args);
$term_id_1st = reset($term_id_1st);
// Get last product category term Id
$query_args['order'] = 'DESC';
$term_id_end = get_terms($query_args);
$term_id_end = reset($term_id_end);
$next_term_id = $term_id_end == $term_id ? $term_id_1st : $term_id + 1;
$prev_term_id = $term_id_1st == $term_id ? $term_id_end : $term_id - 1;
$next_term_link = get_term_link( $next_term_id, $taxonomy );
$next_term = get_term( $next_term_id, $taxonomy );
$prev_term_link = get_term_link( $prev_term_id, $taxonomy );
$prev_term = get_term( $prev_term_id, $taxonomy );
?>
<p><?php echo $prev_term->name; ?></p>
<p><?php echo $next_term->name; ?></p>
<?php endif; ?>
To get the last category you can try the following
I am using ACF to build a product catalogue.
I need to create a list like that:
+Category name
-list
-of
-products
-in
-that
-category
+Another category name
-list
-of
-...
-in the matching category
For all of the categories.
Categories names are the terms of the 'product_categories' taxonomy. The products belong to a custom post type 'products'.
I've already build a loop that displays links to all of the categories with names and images:
$terms = get_terms("product_categories", array('hide_empty' => false));
if ( !empty( $terms ) && !is_wp_error( $terms ) ){
foreach ( $terms as $term ) {
echo '<a href="'.get_term_link($term).'">';
echo '<p>'.$term->name.'</p>';
echo '<img src="'.get_field('image-field-name', $term).'"></a></div>';
}
}
It works fine.
Then I rebuilded it to display the thing i want:
$terms = get_terms("product_categories", array('hide_empty' => false));
if ( !empty( $terms ) && !is_wp_error( $terms ) ){
foreach ( $terms as $term ) {
$category= $term->name;
echo '<p>'.$category.':</p>';
product_list($category);
echo '<br/><br/>';
}
}
function product_list($category_name){
$inner_args=array(
'post_type' => 'products',
'product_categories' => $category_name
);
$my_query = null;
$my_query = new WP_Query($inner_args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php echo '<br/>Product: '.get_field('name_of_the_product');
?>
<?php
endwhile;
}
}
It almost works. The problem is that it displays all of the product names BUT only for the first category, after all of the products from the first category in loop are echoed, following categories are empty (just the category names, no matching items).How can I make it display products in all of the categories, not just the first one? And what's wrong with the code?
Have you tried adding wp_reset_query(); before the end of the foreach loop?
More info http://codex.wordpress.org/Function_Reference/wp_reset_query
Hi Guys, I'm trying to display a taxonomy field called clpr_store_video in my Wordpress sidebar for each store is it possible to call the field value outside the loop on the sidebar, but yet attached to the post id?
I don't really know if its possible or how to do that.
EDITED
What I've tried from another tutorial but no luck.
$queried_object = get_queried_object();
$taxonomy = $queried_object->taxonomy;
$term_id = $queried_object->term_id;
$subheading = get_field('clpr_store_video', $taxonomy . '_' . $term_id);
echo $subheading;
See code block to display taxonomy associated with post and list of taxonomies and their links:
If you wish to get taxonomy associated with particular post
$terms = get_the_terms( get_the_ID(), 'clpr_store_video' );
foreach ( $terms as $term ) {
echo $subheading = $term->name;
}
If you wish to get list and link of all taxonomies
<ul>
<?php
$taxonomies = get_terms('clpr_store_video', array("fields" => "all"));
foreach ($taxonomies as $taxonomy) {
echo '<li>'.$taxonomy->name.'</li>';
}
?>
</ul>
In Wordpress I created a taxonomy called "Cities" for a CPT called "People". I want to display all taxonomy tags selected for a person in the template of the single.php-file.
What is the code for displaying only those elements selected?
I use the following code in the single-people.php:
<?php
$taxonomy = 'cities';
$terms = get_terms($taxonomy);
if ( $terms && !is_wp_error( $terms ) ) :
?>
<ul>
<?php foreach ( $terms as $term ) { ?>
<li><?php echo $term -> name; ?></li>
<?php
} ?>
</ul>
<?php endif; ?>
I want only the selected taxonomy tags without link.
I understand you want to display all the terms of a taxonomy selected for a single post.
You need to use the function wp_get_post_terms() like this:
$taxonomy = 'cities';
$post_terms = wp_get_post_terms( $post->ID, $taxonomy, array( "fields" => "names" ) );
foreach ( $post_terms as $term ) {
echo $term;
}
This will print all the names of the cities the current post is related to. Now you have to adapt this code to your page and, if you don't want links, just do not use <a> tags...
$wrap = 'hello';
$terms = wp_get_post_terms(get_the_ID( ), $wrap, array("fields"=>"names"));
foreach ( $terms as $term ) {
echo $term;
}
in the place of hello you take the taxonomy name and it surely run
I'm trying to get the ID of the category on a category archive page of a custom post type. So far, I'm using the code below, but it doesn't seem to be working. This code is in my taxonomy-{taxonomy}.php file.
$cat_name = single_cat_title('', false);
$cat_id = get_cat_ID($cat_name);
// $cat_name = 'Category Name', which works fine but,
// $cat_id = 0, which is obviously not the id of the category
Do I need to do something special for retrieving the ID of a category of a custom post type?
As a side note, I need this, so I can pass the ID into the get_categories() function
$args = array(
'child_of' => $cat_id,
'taxonomy' => 'taxonomy'
);
$categories = get_categories($args);
There are several ways to get the category id
$category = get_category( get_query_var( 'cat' ) );
$cat_id = $category->cat_ID;
OR
$category = get_the_category();
$cat_id = $category[0]->cat_ID; // or foreach through
OR
if(is_category()) { $cat_ID = get_query_var('cat'); }
OR just
var_dump($wp_query->get_queried_object())
will give the current object for the template like for query on a category archive this is the category object
OR even none of these worked then here is the custom query
global $wpdb;
$category=$wpdb->get_results("SELECT * FROM `wp_terms` WHERE `name` ='$cat_name'");
$category[0]->term_id;
wp_get_post_categories can only get POST categories not a custom post's categories, try this instead:
$category = get_the_terms( $post->ID, 'custom-taxonomy-here' ); //////find custom taxonomy category name
foreach ( $category as $cat){
echo $cat->name;
}
http://wordpress.org/support/topic/wp_get_post_categories-equivalent-for-custom-taxonomies