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'];
Related
So I had a shortcode working well which pulled in the subcategories of a product and displayed the image and text but realised it was at the top of the content because I had used echo . . . so moved the HTML output into a variable so I could return it but the images are coming out of the list items, so seem to be having an issue with the function: woocommerce_subcategory_thumbnail()
Not too sure why but I presume the function must have an echo? I guess I want to just get the image url and put it in a container? Honestly have no idea what the best method is but this is where I'm at
add_shortcode( 'show_products_categories_os', 'categories_of_the_product_os' );
function categories_of_the_product_os() {
$term_id = get_term_by( 'slug', 'os', 'product_cat' );
$terms = get_the_terms( get_the_ID(), 'product_cat' );
if ( $terms ) {
$output_html .= '<ul class="product-cats osp">';
foreach ( $terms as $term ) {
if($term->parent === $term_id->term_id){
$output_html .= '<li class="category os">';
$output_html .= '' . woocommerce_subcategory_thumbnail( $term ) . '';
$output_html .= '<h2>' . $term->name . '</h2>';
$output_html .= '</li>';
}
}
$output_html .= '</ul>';
}
return $output_html;
}
Is there another function I can't find that can give me jst the url for the image? Or a way of stripping it from that other function?
You need to get the thumbnail_id from the term's metadata and pass that as a parameter to wp_get_attachment_url
add_shortcode( 'show_products_categories_os', 'categories_of_the_product_os' );
function categories_of_the_product_os() {
$term_id = get_term_by( 'slug', 'os', 'product_cat' );
$terms = get_the_terms( get_the_ID(), 'product_cat' );
if ( $terms ) {
$output_html .= '<ul class="product-cats osp">';
foreach ( $terms as $term ) {
$thumbnail_id = get_term_meta( $term->term_id, 'thumbnail_id', true );
$image_url = wp_get_attachment_url( $thumbnail_id );
if($term->parent === $term_id->term_id){
$output_html .= '<li class="category os">';
$output_html .= '' . $image_url . '';
$output_html .= '<h2>' . $term->name . '</h2>';
$output_html .= '</li>';
}
}
$output_html .= '</ul>';
}
return $output_html;
}
Source: https://stackoverflow.com/a/51465602/14481105
To Note
You now need to add that $image_url to the src attribute of an img tag for an image to be output, as currently just the URL would appear.
I have this code for the tag list in Wordpress:
$tags_list = get_the_tag_list( '', __( '</li><li>', 'wp-theme') );
if ( $tags_list ) {
printf( '' . __( '<ul><li>%1$s</li></ul>', 'wp-theme' ) . '', $tags_list );
}
It becomes this HTML:
<ul>
<li><a href="http://internal-link/tag1/>TAG NAME 1</a></li>
<li><a href="http://internal-link/tag2/>TAG NAME 2</a></li>
</ul>
But I need to get this:
<ul>
<li>img</li>
<li>img</li>
</ul>
How should I edit the code above to add the external link after each tag and how do I get the tag name without its own link, so I can add it to the external link?
Thank you!
Instead of using get_the_tag_list(), you can manually generate the output:
$terms = get_the_tags();
if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) { // Check if $terms is OK.
echo '<ul>';
foreach ( $terms as $term ) {
$link = get_term_link( $term );
if ( is_wp_error( $link ) ) {
continue;
}
// Here, just change the URL.
$external_link = 'https://external-link/?search=' . $term->name;
echo '<li>' .
'' . $term->name . '' .
' ' . $term->name . '' .
'</li>';
}
echo '</ul>';
}
And that would replace your existing code:
$tags_list = get_the_tag_list( '', __( '</li><li>', 'wp-theme' ) );
if ( $tags_list ) {
printf( '' . __( '<ul><li>%1$s</li></ul>', 'wp-theme' ) . '', $tags_list );
}
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;
}
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/
I've got the following PHP code that I'm using to output a list of all custom taxonomy values, then group them into alphabetical order by first letter. This is working fine, except that the URL isn't being outputted. Anyone able to help?
<?php
$list = '';
$tags = get_terms( 'film-categories' );
$groups = array();
if( $tags && is_array( $tags ) ) {
foreach( $tags as $tag ) {
$first_letter = strtoupper( $tag->name[0] );
$groups[ $first_letter ][] = $tag;
}
if( !empty( $groups ) ) {
foreach( $groups as $letter => $tags ) {
$list .= '<div class="cat-group"><h3>' . apply_filters( 'the_title', $letter ) . '</h3>';
$list .= '<ul>';
foreach( $tags as $tag ) {
$url = esc_attr( get_tag_link( $tag->term_id ) );
$name = apply_filters( 'the_title', $tag->name );
$list .= '<li>' . $name . '</li>';
}
$list .= '</ul></div>';
}
}
} else $list .= '<p>Sorry, but no tags were found</p>';
echo $list;
?>
I'm afraid you've confused.
According to your second line - you're fetching terms of custom tax and not tags.
$tags = get_terms( 'film-categories' );
Therefore , any function related to tags won't work correctly.
In order to get the url of the term use the get_term_link() function.
Just replace the current line with:
$url = esc_attr( get_term_link( $tag ) );
Should work.