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
Related
I am using two plugins WPCustom Category Image for Be able to insert an image in each category and W4 Post List for list the categories on the screen.
In this moment give me always the last image of categories. I pretend the image of each category corresponding to each ID.
The plugin have one shortcode [term_id] which shows the corresponding category id but i can't use one shortcode inside from another shortcode.
Example - [wp_custom_image_category onlysrc="false" size="full" term_id="[term_id"]]
Any solution?
foreach( get_categories(['hide_empty' => false]) as $category) {
$image = do_shortcode('[wp_custom_image_category onlysrc="false" size="full" term_id="'.$category->term_id.'" ]');
echo $image.'<br/>';
// $id = 4;
$options['template'] =
'[terms]
<div class="col-sm-3 news-cat-links" id="[term_id]">
[term_name]<img src="'.$image.'" /></a>
</div>[/terms]';
}
Go to corresponding plugin files and find the functions of [term_id] shortcode.
below code returns array of custom texanomy named "eventcategory". define $post as global variable .
$terms = get_the_terms( $post->ID, 'eventcategory' );
if ( $terms && ! is_wp_error( $terms ) ) :
$draught_links = array();
foreach ( $terms as $term ) {
$draught_links[] = $term->name;
}
print_r($draught_links);
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...
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 am trying to use custom taxonomy in my custom post. Suppose I have 12 custom post in 3 categories. I only need to display each category for one time. I have used the following code. But it repeated the categories.
<?php
$terms = get_the_terms( $post->ID , 'Categori' );
foreach ( $terms as $term ) {
echo $term->name;
}
?>
Please tell me how can I solved the problem.
You can use array_unique() function to remove duplicate values.
But first you need to store your term values in array.
Here is the full code:
<?php
$terms = get_the_terms( $post->ID , 'Categori' );
$c_terms = array(); //Create empty array
foreach ( $terms as $term ) {
$c_terms[] = $term->name;
}
$unique_cat = array_unique($c_terms);
foreach ($unique_cat as $cat) {
echo $cat;
}
?>
ok, here's what I'm trying to do:
I've got a custom post type called drinks-menu, a taxonomy called drinks-menu-categories, and a page template called template-drinks-menu.php.
The taxonomy has a bunch of terms that are heirarchical - Wine, with children White and Red; Beer, with children Pilsener, Stout, etc...
I want to use one loop to display all the posts from these terms in the same order that they're ordered by in the admin. Here's the code I've got so far:
<?php
$post_type = 'drinks-menu';
// Get all the taxonomies for this post type
$taxonomies = get_object_taxonomies( (object) array('post_type' => $post_type ) );
foreach( $taxonomies as $taxonomy ) :
// Gets every "category" (term) in this taxonomy to get the respective posts
$terms = get_terms( $taxonomy );
foreach( $terms as $term ) :
echo '<h1>'.$term->name.'</h1>';
if ( $term->description !== '' ) {
echo '<div class="page_summary">'. $term->description .'</div>';
}
echo '<br>';
$posts = new WP_Query( "taxonomy=$taxonomy&term=$term->slug&posts_per_page=-1&orderby=id&order=DESC" );
if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post();
?>
<?php the_title(); ?>
<?php
if( get_field( "price" ) ): ?>
<?php echo '<span">'; the_field( "price" ); echo ' | '; the_field( "abv" ); echo '</span>';?>
<?php endif;
echo '<em>'; the_excerpt(); echo '</em><br><br>';
endwhile; endif;
endforeach;
endforeach;
?>
This is working well to bring all the posts from the terms onto the page, but not in order. You can see I tried taxonomy=$taxonomy&term=$term-slug&posts_per_page=-1&orderby=id&order=DESC but it's not working, everything shows in alphabetical order.
Any Wordpress gurus who can point me in the right direction? I hope I've been clear about the issue. Thanks :-)
Posts ordered in admin page by "menu_order";
If u want to order posts using query_posts (WP_Query constructor) u should use value of variable "orderby" in upper case -> "ID".