Woocommerce multiple categories - Which one is displayed? - wordpress

I have a problem with Woocommerce. My shop page shows the product categories. But when I have a product in multiple categories, there is only one category displayed, and it seems to be the first one alphabetically.
Instead, I need Woocommerce to display the highest category (or in my case, the lowest category ID, since I organised created the categories in the right order). I have included a link. http://www.jointdeseuil.fr/
Please, can anyone help? Currently the code is like this :
<?php
list($firstpart) = explode('|', $product_cats);
echo $firstpart;
?>

Impossible to know what the rest of your code is.... what is $product_cats? what is $firstpart?
Without that, I'll show how to get the product categories from scratch using wc_get_product_terms()
global $product;
$product_cats = wc_get_product_terms( $product->id, 'product_cat', array( 'orderby' => 'menu_order', 'fields' => 'all' ) );
if( $product_cats ) {
echo '<ul>';
foreach( $product_cats as $cat ){
printf( '<li>%s</li>', esc_url( get_term_link( $cat ) ), $cat->name );
}
echo '</ul>';
}
or if you don't need any special ordering you can use a default WordPress function
global $product;
echo get_the_term_list( $product->id, 'product_cat', __( 'Categories: ', 'text-domain' ), ', ' );

Related

Display Wordpress Parent Category with siblings and current category posts

Here's an example of what I'm trying to do:
This is for a sidebar navigation. When the visitor is on the subcategory I need the subcategory to display the current posts under that category and also the sibling subcategories. This structure will also have to work at the post level as well.
When the visitor is at the category level - I have it set up so that the user is only able to see the subcategories under that category - which is what I need exactly. I'm just trying to sort out the subcategory template (which is its own template - separate from the category template).
If there’s a plugin that addresses this issue quickly, I’m all for it. Right now I’m just trying to use wp_list_categories with no luck. I appreciate all your help! Thanks!
<?php
$taxonomy_name = 'products-category';
$args = array(
'type' => 'products',
'orderby' => 'name',
'order' => 'ASC',
'taxonomy' => $taxonomy_name,
'parent' => '0',
'hide_empty' => 0
);
$categories = get_categories( $args );
foreach($categories as $category){
echo $category->name;
echo $term_id = $category->term_id.'<br>';
$termchildren = get_term_children( $term_id, $taxonomy_name );
echo '<ul>';
foreach ( $termchildren as $child ) {
$term = get_term_by( 'id', $child, $taxonomy_name );
echo '<li>' . $term->name . '</li>';
// Note : Here is your Post loop using term id
}
echo '</ul>';
}
?>
Good Luck

Getting the Product Attribute Term Permalink in WordPress / WooCommerce

I'm using WooCommerce with WordPress to build a store of Antique Photos.
I'm using the WooCommerce product attributes feature to store information about an item photographer.
See here for an example, photographer attribute is pulled out in the box on the left http://bit.ly/1Dp999O
The code that pulls out the attribute looks like this:
if($attribute['is_taxonomy']) {
$values=wc_get_product_terms($product->id, $attribute['name'], array('fields' => 'names'));
echo apply_filters('woocommerce_attribute', wpautop(wptexturize(implode(', ', $values))), $attribute, $values);
}
$values looks like this:
Array ( [0] => Photographer 1 )
The problem is, how do I get to the permalink for the Photographer which is automatically generated by WordPress and WooCommerce:
http://bit.ly/1JtwBna
I can't find any documentation for this within WooCommerce, and this seems to be a taxonomy within a taxonomy which goes a bit further than stabdard WordPress, but I'm assuming this is a fairly standard requirement. Any pointers appreciated.
Once you have the attribute term (in this case the photographer's name) you can use get_term_link() to get the URL. Because the $product id isn't passed to the woocommerce_attribute folder, I couldn't filter that, but instead create an override of the product-attributes.php template. And modified the relevant section to be the following:
if ( $attribute['is_taxonomy'] ) {
$terms = wc_get_product_terms( $product->id, $attribute['name'], array( 'fields' => 'all' ) );
$html = '';
$counter = 1;
$total = count( $terms );
foreach( $terms as $term ){
$html .= sprintf( '%s',
esc_url( get_term_link( $term ) ),
esc_attr( $term->name ),
wptexturize( $term->name )
);
if( $counter < $total ){
$html .= ', ';
}
$counter++;
}
echo wpautop( $html );
}
For some reason, the URL isn't a pretty permalink. It's late and I can't tell if this has to do with my configuration or what exactly, but it is a start.
This basically does what I require:
$terms = get_the_terms($product->id, $attribute['name']);
foreach ($terms as $term){
echo ''.$term->name.'';
}
But could do with a bit of refinement for sure.

wordpress show sub categories in parent category

I can see there is already a lot of information on this, but I can't seem to find anything up to date and just wondered if someone can help me.
I have different parent categories and sub-categories, for example:
Web Hosting
Reviews
Coupons
Domain Registrars
Top registrars
Discount Codes
I am using the following code in the category.php page and it displays the sub-categories in each category fine:
<?php
if ( is_category() ) {
$this_category = get_category($cat);
if($this_category->category_parent):
else:
$this_category = wp_list_categories('orderby=id&depth=5&show_count=0&title_li=&use_desc_for_title=1&child_of='.$this_category->cat_ID."&echo=0");
echo '<ul>'. $this_category . '</ul>';
endif;
}
?>
But when I click on a sub-category link it displays all the posts in that sub-category fine, but then there are obviously no links back to the parent directory etc.
Is there anyway of doing this? Does anyone have some code for me that doesn't have any bugs? Thanks a lot.
You can modify your code to something like this:
<?php
if ( is_category() ) :
$category = get_category( $cat );
if ( $category->category_parent ) : // if category has parent
$category_parent_id = $category->category_parent;
$category_parent_link = get_category_link( $category_parent_id );
echo '' . get_category( $category_parent_id )->name . '';
else : // else category has children
$children = wp_list_categories( array(
'child_of' => $category->cat_ID,
'depth' => 5,
'echo' => 0,
'orderby' => 'id',
'title_li' => '',
) );
echo '<ul>' . $children . '</ul>';
endif;
endif;
This is one way of doing it. There are other ways too. I can suggest these functions for that:
get_category_parents()
get_term_children()

Wordpress Custom Post category archive

I have a custom post type set up, with a number of categories and sub categories.
What I am trying to do is create a page that shows all the posts in a specific category, with a menu that lists all the category sub categories so the posts can then be filtered.
I have tried copying the archive template and renaming it taxonomy-(my-custom-taxonomy).php which then if I go to the slug shows certain posts, and using
<?php wp_list_categories(); ?> but I just want a list of all the sub-categories of a specific category, and to filter those posts. I am struggling to show these and use one template for all categories and children.
You can use
$term_id = get_queried_object()->term_id;
and
$tax= get_query_var( 'taxonomy' )
to return the details of the current term and taxonomy being viewed in your taxonomy.php page.
You can then use that info with get_term_children to get the child terms of the current term being displayed. For examples, see the link provided
EDIT
Your code should look like this
<?php
$term_id = get_queried_object()->term_id;
$taxonomy_name = get_query_var( 'taxonomy' );
$termchildren = get_term_children( $term_id, $taxonomy_name );
foreach ( $termchildren as $child ) {
echo '<ul>';
$term = get_term_by( 'id', $child, $taxonomy_name );
echo '<li>' . $term->name . '</li>';
$args = array(
'tax_query' => array(
array(
'taxonomy' => $taxonomy_name,
'field' => 'slug',
'terms' => $term->slug,
),
),
);
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<p>' . get_the_title() . '</p>';
}
wp_reset_postdata();
echo '</ul>';
}
?>

Only displaying a posts selected taxonomy terms?

When using wp_get_post_terms() I can produce a list of taxonomy terms associated with a post. However, I only want to show the taxonomy terms that have been selected for that post. Using the aforementioned function and get_terms() will successfully find the taxonomy terms, but it will show all of the terms. Not only the ones that have been selected. In the $args array for the functions I've looked for a 'selected' filter, but I found none and when I tried it, it didn't work.
Am I trying to do something that can't be done? I'm sure it's something that is starring me right in the face. I just want to ask the pro's before I make major changes to the way I'm doing things.
wp_get_post_terms only returns terms that have been selected for that post, it doesn't return all taxonomy terms.
http://codex.wordpress.org/Function_Reference/wp_get_post_terms
<?php
$the_selected = $_GET['cat'];
$args = array( 'post_type' => 'portfolio_item', 'posts_per_page' => 11, 'orderby' => 'id', 'order' => 'DESC', 'themes_categories' => "$the_selected");
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
This works well for me. I simply send the taxonomy slug to browser, and itterate through them with the code above.
I send by this:
<li>Filter By:</li>
<?php
$categories=get_categories($args);
foreach($categories as $category) {
echo '<li><a href="' . get_category_link( $category->term_id ) . '?cat=' . $category->slug.'" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </li> ';
}
?>
You can try out this code, it worked for me. I have a taxonomy named 'stores' and i wanted to display 2 selected taxonomy from it. so i used a include feature.
<?php
$taxonomy = 'stores';
$args1=array(
'include'=> array(12,30)
);
$terms = get_terms('stores',$args1 );
echo '<ul>';
foreach ($terms as $term) {
//Always check if it's an error before continuing. get_term_link() can be finicky sometimes
$term_link = get_term_link( $term, 'stores' );
if( is_wp_error( $term_link ) )
continue;
//We successfully got a link. Print it out.
echo '<li>' . $term->name . '</li>';
}
echo '</ul>';
?>
<?php echo get_the_term_list( $post->ID, 'your_taxonamy'); ?>
And if you want it without the term linking you can use this
<?php $terms_as_text = get_the_term_list( $post->ID,'your_taxonamy'); if (!empty($terms_as_text)) echo '', strip_tags($terms_as_text) ,''; ?>

Resources