I've got the following snippet to grab all the terms of the taxonomy available for the post.
$tags = get_the_terms( $post->ID, 'books' );
if( $tags ) : ?>
<div class="listing-tag-list">
<?php foreach( $tags as $tag ) :
$tag_link = esc_url( get_term_link( $tag ) );
$tag_output = '';
$tag_output .= '<a href="' . $tag_link . '" class="listing-tag">';
$tag_output .= '<span class="tag__text">' . $tag->name . '</span></a>';
echo $tag_output;
endforeach; ?>
</div>
<?php endif;
My problem is that currently, the terms show in a row without a space.
How can I separate them with a space and a comma?
I've been trying to use implode and so replace the echo $tag_output; with echo implode( ', ', $tag_output );, but I can't seem to be able to fit it into the current code.
Where am I going wrong?
Why not add it directly in your loop
$tag_output = '';
$tag_output .= '<a href="' . $tag_link . '" class="listing-tag">';
$tag_output .= '<span class="tag__text">' . $tag->name . '</span></a>';
$tag_output .=", ";
echo $tag_output;
And outside the loop,remove the last comma
rtrim($tag_output,', ');
Easy and simple solution:
<?php echo get_the_terms_list( $post_id, 'books', '<div class="listing-tag-list">', ', ', '</div>' );?>
More details on codex
Or this one:
<?php
$tags = get_the_terms( $post->ID, 'books' );
if( ! is_wp_error( $tags ) ){
$links = array();
foreach ( $tags as $tag ) {
$link = get_term_link( $tag, $tag );
$links[] = '<span class="tag__text">' . $tag->name . '</span>';
}
$before = '<div class="listing-tag-list">';
$sep = ', ';
$after = '</div>';
echo $before . join( $sep, $tag_links ) . $after;
}
Related
I´m trying to display a custom taxonomy image in the frontend on a single post.
Following situation:
Created a CPT for "Weine"
Added a taxonomy "winzer"
Added the following code to my functions.php to display the "winemaker" taxonomy with its description in the single post using a shortcode:
function wpb_catlist_desc() {
$string = '<div>';
$catlist = get_terms( 'winzer' );
if ( ! empty( $catlist ) ) {
foreach ( $catlist as $key => $item ) {
$string .= '<div>'. $item->name . '<br />';
$string .= '<em>'. $item->description . '</em></div>';
}
}
$string .= '</div>';
return $string;
}
add_shortcode('wpb_categories', 'wpb_catlist_desc');
Added an Image fieled to the "winzer" taxonomy using ACF. --> Here´s where I´m stuck now.
I need to add another line to the php snippet to also display the image using the shortcode.
Any hints how to get this done? :)
Cheers!!!!
function wpb_catlist_desc() {
$string = '<div>';
$catlist = get_terms( 'winzer' );
if ( ! empty( $catlist ) ) {
foreach ( $catlist as $key => $item ) {
$image = get_field('Your_image_field_ID');
if(!empty($image)) {
$imgurl = $image['url'];
}
$string .= '<div>';
$string .= '<img src="' . $imgurl . '" alt="' . $item->name . '"/><br />';
$string .= $item->name . '<br />';
$string .= '<em>'. $item->description . '</em></div>';
}
}
$string .= '</div>';
return $string;
}
add_shortcode('wpb_categories', 'wpb_catlist_desc');
Here's how I got it solved:
function wpb_catlist_desc() {
global $post;
$string = '<div>';
$catlist = wp_get_post_terms($post->ID, 'winzer');
if ( ! empty( $catlist ) ) {
foreach ($catlist as $item ) {
$image = get_field('winzer_bild', 'winzer_'.$item->term_id);
if(!empty($image)) {
$imgurl = $image['url'];
}
$string .= '<div>';
$string .= '<img class="winzer-bild-klein" src="' . $imgurl . '" alt="' . $item->name . '"style="width:100%;"/>';
$string .= '<h3 class="winzer-name-n">'. $item->name . '</h3>';
$string .= '<p>'. $item->description . '</p></div>';
}
}
$string .= '</div>';
return $string;
}
add_shortcode('wpb_categories', 'wpb_catlist_desc');
I've got the following code which is a part of the widget that outputs the terms of the taxonomy 'season'
The taxonomy terms are output with space and comma in between them, but it also adds a comma at the very end.
How can I get rid off the last comma?
echo $args['before_widget'];
if ( ! empty( $title ) )
echo $args['before_title'] . $title . $args['after_title'];
global $post;
$tags = get_the_terms( $post->ID, 'season' );
if( $tags ) : ?>
<?php foreach( $tags as $tag ) :
$tag_link = esc_url( get_term_link( $tag ) );
$tag_output = '';
$tag_output .= '<a href="' . $tag_link . '" class="listing-tag">';
$tag_output .= '<span class="tag__text">' . $tag->name . '</span></a>';
$tag_output .=", ";
echo $tag_output;
endforeach; ?>
<?php endif;
echo $args['after_widget'];
}
I'been trying to use the rtrim($tag_output,', '); but I just can't figure out where to put this rtrim string, to make this working.
Where in the code should the rtrim($tag_output,', '); sit to make this work?
It might be easier to map your array to one containing the strings you want and then display it using implode(). For example
if ($tags) {
$tagOutput = array_map(function($tag) {
return sprintf(
'<span class="tag__text">%s</span>',
esc_url( get_term_link( $tag ) ),
$tag->name
);
}, $tags);
echo implode(', ', $tagOutput);
}
echo $args['after_widget'];
I found the following code to display all custom attributes on a product detail page (with a specific bar-style design that I need). The code works like a charm and I have the proper CSS to display horizontal bars of my custom attributes.
Problem I have is that I only want to display specific named
attributes and don't know how to change the loop to do that...
function isa_woocommerce_all_pa(){
global $product;
$attributes = $product->get_attributes();
if ( ! $attributes ) {
return;
}
$out = '<ul class="taste-attributes">';
foreach ( $attributes as $attribute ) {
// skip variations
if ( $attribute->get_variation() ) {
continue;
}
$name = $attribute->get_name();
if ( $attribute->is_taxonomy() ) {
$terms = wp_get_post_terms( $product->get_id(), $name, 'all' );
// get the taxonomy
$tax = $terms[0]->taxonomy;
// get the tax object
$tax_object = get_taxonomy($tax);
// get tax label
if ( isset ( $tax_object->labels->singular_name ) ) {
$tax_label = $tax_object->labels->singular_name;
} elseif ( isset( $tax_object->label ) ) {
$tax_label = $tax_object->label;
// Trim label prefix since WC 3.0
if ( 0 === strpos( $tax_label, 'Product ' ) ) {
$tax_label = substr( $tax_label, 8 );
}
}
$out .= '<li class="' . esc_attr( $name ) . '">';
$out .= '<p class="attribute-label">' . esc_html( $tax_label ) . ': </p> ';
$tax_terms = array();
foreach ( $terms as $term ) {
$single_term = esc_html( $term->name );
// Insert extra code here if you want to show terms as links.
array_push( $tax_terms, $single_term );
}
$out .= '<span class="attribute-value">' . implode(', ', $tax_terms) . '</span><progress value="' . implode(', ', $tax_terms) .
'" max="10"><div class="progress-bar"><span style="width:'
. implode(', ', $tax_terms) . '0%">'
. implode(', ', $tax_terms) . '</span></div></progress></li>';
} else {
$value_string = implode( ', ', $attribute->get_options() );
$out .= '<li class="' . sanitize_title($name) . ' ' . sanitize_title( $value_string ) . '">';
$out .= '<p class="attribute-label">' . $name . ': </p> ';
$out .= '<progress value="' . esc_html( $value_string ) . '" max="10"></progress></li>';
}
}
$out .= '</ul>';
echo $out;
}
add_action('woocommerce_single_product_summary', 'isa_woocommerce_all_pa', 20);
In the following code you will define first the desired product attributes slugs in an array, that will get displayed in single product pages:
add_action( 'woocommerce_single_product_summary', 'display_some_product_attributes', 25 );
function display_some_product_attributes(){
// HERE define the desired product attributes to be displayed
$defined_attributes = array('fyllighet', 'carrier', 'billing-e-number');
global $product;
$attributes = $product->get_attributes();
if ( ! $attributes ) {
return;
}
$out = '<ul class="taste-attributes">';
foreach ( $attributes as $attribute ) {
// Get the product attribute slug from the taxonomy
$attribute_slug = str_replace( 'pa_', '', $attribute->get_name() );
// skip all non desired product attributes
if ( ! in_array($attribute_slug, $defined_attributes) ) {
continue;
}
// skip variations
if ( $attribute->get_variation() ) {
continue;
}
$name = $attribute->get_name();
if ( $attribute->is_taxonomy() ) {
$terms = wp_get_post_terms( $product->get_id(), $name, 'all' );
// get the taxonomy
$tax = $terms[0]->taxonomy;
// get the tax object
$tax_object = get_taxonomy($tax);
// get tax label
if ( isset ( $tax_object->labels->singular_name ) ) {
$tax_label = $tax_object->labels->singular_name;
} elseif ( isset( $tax_object->label ) ) {
$tax_label = $tax_object->label;
// Trim label prefix since WC 3.0
if ( 0 === strpos( $tax_label, 'Product ' ) ) {
$tax_label = substr( $tax_label, 8 );
}
}
$out .= '<li class="' . esc_attr( $name ) . '">';
$out .= '<p class="attribute-label">' . esc_html( $tax_label ) . ': </p> ';
$tax_terms = array();
foreach ( $terms as $term ) {
$single_term = esc_html( $term->name );
// Insert extra code here if you want to show terms as links.
array_push( $tax_terms, $single_term );
}
$out .= '<span class="attribute-value">' . implode(', ', $tax_terms) . '</span><progress value="' . implode(', ', $tax_terms) .
'" max="10"><div class="progress-bar"><span style="width:'
. implode(', ', $tax_terms) . '0%">'
. implode(', ', $tax_terms) . '</span></div></progress></li>';
} else {
$value_string = implode( ', ', $attribute->get_options() );
$out .= '<li class="' . sanitize_title($name) . ' ' . sanitize_title( $value_string ) . '">';
$out .= '<p class="attribute-label">' . $name . ': </p> ';
$out .= '<progress value="' . esc_html( $value_string ) . '" max="10"></progress></li>';
}
}
$out .= '</ul>';
echo $out;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
I am working on project and I need some group help.
I am using woocommerce product system and on shop archive page product I am showing attribute-label: attribute-value (just like text).
attribute-label:attribute-value (ex. Transmission: Manual)
Is there a way to show attribute-label as image? I can't add html code like <img src..."/> and styling with CSS doesn't help too.
I have searched for plugins and more.. But nothing on web.
To show attributes on product shop page i have used this function:
function isa_woocommerce_all_pa(){
global $product;
$attributes = $product->get_attributes();
if ( ! $attributes ) {
return;
}
$out = '<ul class="custom-attributes">';
foreach ( $attributes as $attribute ) {
// skip variations
if ( $attribute->get_variation() ) {
continue;
}
$name = $attribute->get_name();
if ( $attribute->is_taxonomy() ) {
$terms = wp_get_post_terms( $product->get_id(), $name, 'all' );
// get the taxonomy
$tax = $terms[0]->taxonomy;
// get the tax object
$tax_object = get_taxonomy($tax);
// get tax label
if ( isset ( $tax_object->labels->singular_name ) ) {
$tax_label = $tax_object->labels->singular_name;
} elseif ( isset( $tax_object->label ) ) {
$tax_label = $tax_object->label;
// Trim label prefix since WC 3.0
if ( 0 === strpos( $tax_label, 'Product ' ) ) {
$tax_label = substr( $tax_label, 8 );
}
}
$out .= '<li class="' . esc_attr( $name ) . '">';
$out .= '<span class="attribute-label">' . esc_html( $tax_label ) . ': </span> ';
$out .= '<span class="attribute-value">';
$tax_terms = array();
foreach ( $terms as $term ) {
$single_term = esc_html( $term->name );
// Insert extra code here if you want to show terms as links.
array_push( $tax_terms, $single_term );
}
$out .= implode(', ', $tax_terms);
$out .= '</span></li>';
} else {
$value_string = implode( ', ', $attribute->get_options() );
$out .= '<li class="' . sanitize_title($name) . ' ' . sanitize_title( $value_string ) . '">';
$out .= '<span class="attribute-label">' . $name . ': </span> ';
$out .= '<span class="attribute-value">' . esc_html( $value_string ) . '</span></li>';
}
}
$out .= '</ul>';
echo $out;
}
add_action('woocommerce_single_product_summary', 'isa_woocommerce_all_pa', 25);
Can someone help me with this? How to change attribute-label with image?
Updated since WC 3.2+
As Product attributes dont have images, you should create a in your active theme a folder images (if it doesn't exist) and inside a subfolder attributes.
For each product attribute, you will have to add an image inside this subfolder attributes, which name will be the name of your attribute (the slug). For example for "Color" attribute you will have to add an image named color.jpg.
Then I have make some changes in your code, to get this working. Only the terms set in the product for each attribute will be displayed. For each attribute you will get an image.
Here is the code:
add_action('woocommerce_single_product_summary', 'isa_woocommerce_all_pa', 25);
function isa_woocommerce_all_pa(){
global $product;
$attributes = $product->get_attributes();
if ( ! $attributes ) return;
$out = '<ul class="custom-attributes">';
foreach ( $attributes as $attribute ) {
if ( $attribute->get_variation() ) continue; // skip variations
if ( $attribute->is_taxonomy() ) {
$taxonomy = $attribute->get_name();
$taxo_obj = $attribute->get_taxonomy_object();
$name = $taxo_obj->attribute_name; // <== Corrected
$label = $taxo_obj->attribute_label; // <== Corrected
$out .= '<li class="' . esc_attr( $taxonomy ) . '">';
## ATTRIBUTE IMAGE ##
// For a child theme use get_stylesheet_directory_uri() instead.
$out .= '<img class="attribute-image" src="'.get_template_directory_uri().'/images/attributes/'.$name.'.jpg" alt="Attribute '.$label.'"/> ';
$out .= '<span class="attribute-values">';
$terms = wp_get_post_terms( $product->get_id(), $taxonomy, array('fields' => 'names') );
foreach ( $terms as $term_name )
$term_names['name'] = $term_name;
$out .= implode(', ', $term_names);
$out .= '</span></li>';
} else {
$value_string = implode( ', ', $attribute->get_options() );
$out .= '<li class="' . sanitize_title($taxonomy) . ' ' . sanitize_title( $value_string ) . '">';
$out .= '<span class="attribute-label">' . $taxonomy . ': </span> ';
$out .= '<span class="attribute-value">' . esc_html( $value_string ) . '</span></li>';
}
}
$out .= '</ul>';
echo $out;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and work.
I want to display all terms with there image of particular taxonomy. i got all term details using get_term() function.
<?php
$terms = get_terms( 'vehicle_type' );
foreach ($terms as $term) :
echo $term->slug;
$colors = apply_filters( 'taxonomy-images-get-terms', '', array(
'taxonomy' => 'vehicle_type',
'term_args' => array(
'slug' => $term->slug,
)
)
);
foreach( (array) $colors as $color) :
echo wp_get_attachment_image( $color->image_id, 'full', array('class' => 'alignnone'));
//echo $term->name;
endforeach;
endforeach;
?>
But it is showing same path for all images.
http://localhost/mototrader/wp-includes/images/media/default.png
How could i get actual path of image associated to that taxonomy.
Thanks in advance.
You can try by this way also
<?php
$cat_id = get_query_var('cat');
$catlist = get_categories('hide_empty=0&child_of=' . $cat_id);
echo "<ul>";
foreach($catlist as $categories_item)
{
echo '<h1><a href="' . get_category_link( $categories_item->term_id ) . '" title="' . sprintf( __( "View all products in %s" ), $categories_item->name ) . '" ' . '>' . $categories_item->name.'</a> </h1> ';
echo '<div class="categoryoverview clearfix">';
$terms = apply_filters( 'taxonomy-images-get-terms', '' );
if ( ! empty( $terms ) ) {
foreach( (array) $terms as $term ) {
if($term->term_id == $categories_item->term_id) {
print '<a href="' . esc_url( get_term_link( $term, $term->taxonomy ) ) . '">' . wp_get_attachment_image( $term->image_id, 'thumbnail' );
echo '</a>';
}
}
echo '<p>'. $categories_item->description; echo '</p>';
}
echo '</div>';
}
echo "</ul>";
You can Add Advance custom field
<< using that create image field.
<< Assign that image each taxonmoy
<< you can easily get image of corresponding taxonmy image.
Refer this >> https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/