I have created attribute ( Product Type, Slug: product-type, Type: label )
Product Type have few teksonomy like: Roof, Doors etc...
In product i was set Attribute and select Roof, and this is show in TAB attribute, but i want in my product page in section of Breadcrumb NavXT Was to show this Attribute from product-type if exist for this product.
Code from template where is for this Breadcrumb NavXT section
**Update, code is almost what i need:
$product = wc_get_product();
$attributes = $product->get_attributes();
if (array_key_exists('pa_typ-produktu', $attributes)) {
$attribute = $attributes['pa_typ-produktu'];
$name = $attribute->get_name();
$values = $attribute->get_options();
echo '<strong>'.$name.':</strong> ' . implode(', ', $values) . '<br>';
}
show me:
pa_typ-produktu: 141
But i want see name of this teksonomy not ID.
Nevermaind this code solve my problem:
<?php
$product = wc_get_product();
$taxonomy = 'pa_typ-produktu';
$terms = wp_get_post_terms( $product->get_id(), $taxonomy );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
foreach ( $terms as $term ) {
echo $term->name;
}
}
?>
Related
Im trying to display the current post category name for a custom post types' taxonomy, but exclude one of the categories. I see different solutions in StackOverflow, but I can't seem to get any of them to work for me. Below is the code I am using, which works great, but I don't know how to exclude one ID using it.
<?php $terms = get_the_terms( $post->ID , 'press_category' );
foreach ( $terms as $term ) {
$term_link = get_term_link( $term, 'press_category' );
if( is_wp_error( $term_link ) )
continue;
echo $term->name ;
}
?>
Try this
<?php $terms = get_the_terms($post->ID, 'press_category');
foreach ($terms as $term)
{
$term_link = get_term_link($term, 'press_category');
if (is_wp_error($term_link)) continue;
if ($term->term_id != 222)
{
echo $term->name;
}
}
?>
Change 222 in this line to your taxonomy id for exclude
$term->term_id != 222
I'm looking to show second listed category if one exists, if not, then I want to show the first ( primary category )
Here is what I have so far:
<?php $categories = get_the_category();
if ( ! empty( $categories ) ) {
echo esc_html( $categories[1]->name );
}
else $terms = get_the_terms( $post->ID, 'product_cat' );
if ( $terms && ! is_wp_error( $terms ) ) {
echo $terms[1]->name;
}
if ( ! empty( $terms[1]->name ) ) {
echo $terms[0]->name;
}
?>
so far is will show second, but not fall back to the first listed category.
If I understood your question clearly, you can achieve that with this code:
<?php
$terms = get_the_terms( $post->ID, 'product_cat' );
if ($terms && !is_wp_error($terms)) {
if (!empty($terms[0]->name))
echo $terms[0]->name; // second category
else
echo $terms[1]->name; // first (primary) category
}
?>
I have created fields for terms using ACF. I have also created category.php using the below code:
<?php
// load all 'category' terms for the post
$terms = get_the_terms( get_the_ID(), 'category' );
// we will use the first term to load ACF data from
if ( !empty( $terms ) ) {
$term = array_pop($terms);
if ( have_rows( 'page_banner' ) ) :
// loop through the rows of data
while ( have_rows( 'page_banner' ) ) : the_row();
$custom_field = get_sub_field( 'small_title', $term );
echo $custom_field;
endwhile;
else :
endif;
}
?>
But I am not getting the results, Need your help. Thank you.
I just checked the following code. Use the following code.
<?php
// get the current taxonomy term
$term = get_queried_object();
if ( have_rows( 'page_banner', $term ) ) {
while( have_rows( 'page_banner', $term ) ) {
the_row();
the_sub_field( 'small_title' );
echo '<br />';
}
}
Reference
https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/
https://www.advancedcustomfields.com/resources/have_rows/
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.
I search all the web for that answer. I use wp_list_categories to make a submenu with custom taxonomy, It works well, and puts current-cat when I browse those categories.
The thing is, when I browse single posts with this menu, the highlight no more works.
For the blog part of that site, I use the following code to highlight current category on wp_list_categories():
function sgr_show_current_cat_on_single($output) {
global $post;
if( is_single() ) {
$categories = wp_get_post_categories($post->ID);
foreach( $categories as $catid ) {
$cat = get_category($catid);
if(preg_match('#cat-item-' . $cat->cat_ID . '#', $output)) {
$output = str_replace('cat-item-'.$cat->cat_ID, 'cat-item-'.$cat->cat_ID . ' current-cat', $output);
}
}
}
return $output;
}
add_filter('wp_list_categories', 'sgr_show_current_cat_on_single');
But as far as I tried, can't make it work for single posts that are ordered by custom taxonomy. :/ > I don't know how to custom it.
Is it even possible ?
You need to use get_the_terms( $id, $taxonomy ); instead of wp_get_post_categories(); for getting custom taxonomy term IDs.
You can hardcode taxonomy name into the functon or get it from the $args you passed into wp_list_categories( $args );.
Final code:
add_filter( 'wp_list_categories', 'sgr_show_current_cat_on_single', 10, 2 );
function sgr_show_current_cat_on_single( $output, $args ) {
if ( is_single() ) :
global $post;
$terms = get_the_terms( $post->ID, $args['taxonomy'] );
foreach( $terms as $term ) {
if ( preg_match( '#cat-item-' . $term ->term_id . '#', $output ) ) {
$output = str_replace('cat-item-'.$term ->term_id, 'cat-item-'.$term ->term_id . ' current-cat', $output);
}
}
endif;
return $output;
}