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/
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 Continent -> Country category setup for a custom post type.
- Africa (parent 1)
- Uganda
- Zambia
- Zimbabwe
- Asia (parent 2)
- Afghanistan
- Bahrain
- Bangladesh
- Bhutan
If parent category is checked for a post, don't echo the children categories. (even if one or more children is checked) echo => Africa, Asia
And the revert, if one or more child categories is checked, but the parent category is NOT checked. Show only the child categories. echo => Uganda, Zambia, Zimbabwe, Afghanistan, Bahrain, Bangladesh, Bhutan
UPDATE
Also if Africa (parent 1) is checked, while Asia (parent 2) is NOT checke, but Afghanistan and Bhutan (children of parent 2) is checked the output should be: echo => Africa, Afghanistan, Bhutan.
This will only output IF there is one or more parent categories checked.
<?php
$post = get_post(); // If $post is already available, skip.
$terms = get_the_terms( $post->ID, 'custom-category' );
foreach ( $terms as $term ) :
if ( $term->parent === 0 ) :
echo '<a href="' . esc_url( get_term_link( $term->term_id, 'custom-category' ) ) .
'" title="' . esc_html( $term->name ) . '" ' . '>' . esc_html( $term->name ) .
'</a> ';
endif;
endforeach;
?>
How to output the child catergories if their parent is NOT checked?
Please try below code which help you for logic of the need you may modify it for your output requirements
$post = get_post(); // If $post is already available, skip.
$terms = get_the_terms( $post->ID, 'category' );
$outputparent = $outputchild = array();
foreach( $terms as $term ) :
if( $term->parent === 0 ) :
$outputparent[] = '<a href="' . esc_url( get_term_link( $term ) ) .
'" title="' . esc_html( $term->name ) . '" ' . '>' . esc_html( $term->name ) .
'</a> ';
else :
$outputchild[] = '<a href="' . esc_url( get_term_link( $term ) ) .
'" title="' . esc_html( $term->name ) . '" ' . '>' . esc_html( $term->name ) .
'</a>';
endif; //Endif
endforeach;
if( !empty( $outputparent ) ) :
echo 'Parent category is checked<br>';
echo implode('<br>', $outputparent);
$outputchild = array();
elseif( !empty( $outputchild ) && empty( $outputparent ) ) :
echo 'Only Childs<br>';
echo implode('<br>', $outputchild);
endif;
I manage to figure out a solution to this problem! This is tested and produces my desired result! If you have a more elegant solution, please let me know!
<?php
$categories = get_the_terms( $post->ID, 'custom-category' );
// If term is a parent, add to post_parent array.
$post_parent = array();
foreach( $categories as $parent_id ) {
if($parent_id->parent < 1) {
$post_parent[] = $parent_id->term_id;
}
}
// If terms parentId does not exist in post_parent array
// add to array regions as a key => value pair
$regions = array();
foreach( $categories as $category ) {
if (!in_array($category->parent, $post_parent)) {
$regions[$category->term_id] = $category->name;
}
}
// Sort terms based on keys (regions), impolde and print
ksort($regions);
$locations = array();
foreach($regions as $key => $value) {
$locations[] = ' ' . $value . '';
}
echo implode(",", $locations);
?>
I have found a WooCommerce code snippet that adds sorting on Category pages, and it works.
But the problem is that the sorting subcategory is only displayed on the Parent category page, but not in the subcategories pages.
This is the category page:
The problem is that the sorting is not working on subcategory pages:
What do I need to change in my code to achieve that?
Here is my code:
function tutsplus_product_subcategories( $args = array()) {
$parentid = get_queried_object_id();
$args = array(
'parent' => $parentid
);
$terms = get_terms( 'product_cat', $args );
if ( $terms) {
echo '<ul class="wooc_sclist">';
foreach ( $terms as $term ) {
echo '<li class="category">';
echo '<h2>';
echo '<a href="' . esc_url( get_term_link( $term ) ) . '" class="' . $term->slug . '">';
echo $term->name;
echo '</a>';
echo '</h2>';
echo '</li>';
}
echo '</ul>';
}
}
add_action( 'woocommerce_before_shop_loop', 'tutsplus_product_subcategories', 50 );
Reference: Display WooCommerce Categories, Subcategories, and Products in Separate Lists
Well i found out how to make it so i will share it with you:
function sub_fillter(){
$sub = wp_get_post_terms(get_the_ID(),'product_cat');
if ( is_subcategory()){
$cat = array_shift( $sub );
$cat=get_term_top_most_parent($cat->term_id,'product_cat');
tutsplus_product_subcategories($cat);
}
}
add_action('woocommerce_before_shop_loop', 'sub_fillter', 50);
function is_subcategory (){
$cat = get_query_var('cat');
$category = get_category($cat);
$category_gt_parent="";
return ( $category_gt_parent == '0' ) ? false : true;
}
function tutsplus_product_subcategories( $cat) {
$parentid = $cat->term_id;
$args = array(
'parent' => $parentid
);
$terms = get_terms( 'product_cat', $args );
if ( $terms || is_subcategory()) {
echo '<ul class="wooc_sclist">';
foreach ( $terms as $term ) {
echo '<li class="category">';
echo '<h2>';
echo '<a href="' . esc_url( get_term_link( $term ) ) . '" class="' . $term->slug . '">';
echo $term->name;
echo '</a>';
echo '</h2>';
echo '</li>';
}
echo '</ul>';
}
}
Im currently displaying the custom taxonomy term for my post on a single-resources.php page. However I need it to link to the taxonomy category page and not the link of the page.
This is what I currently have:
<?php
$term_list = wp_get_post_terms($post->ID, 'resourcecategory', array("fields" => "all"));
foreach($term_list as $term_single) {
echo '<a class="icon-hv-link" href="' . esc_url( $term_link ) . '"><i class="icon-left-open-big"></i><span>' . $term_single->name . '</span></a>';
}
?>
I was previously doing this which does work however its displaying every taxonomy term rather than the one specific to the post, so it doesn't work :(
<?php $terms = get_terms( 'resourcecategory' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
foreach ( $terms as $term ) {
echo '<a class="icon-hv-link" href="' . esc_url( $term_link ) . '"><i class="icon-left-open-big"></i><span>' . $term->name . '</span></a>';
}
}?>
Does anyone have any idea on someway to combine the two?
For anyone else having an issue with this I managed to achieve what I was after with the following code:
<?php
$terms = get_the_terms( $post->ID, 'resourcecategory');
foreach($terms as $term) {
echo '<a class="icon-hv-link" href="' . get_term_link($term) . '"><i class="icon-left-open-big"></i><span>' . $term->name . '</span></a>';
}
?>
You need to use get_the_terms instead of get_terms. As mentioned in the comments, dont use wp_get_post_terms as this causes unnecessary calls to the database
I've managed to list out all direct children of the custom parent taxonomy..
Chocolates
Marshmallows
Popcorn
..and so on...
with this code below.
<?php
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
if ($term->parent == 0) {
$terms = get_terms( 'product-type', 'parent='.$term->term_id ); }
else { $terms = get_terms( 'product-type', 'parent='.$term->parent ); }
foreach($terms as $term) {
echo '
<div class="snack_type">
' . $term->name . '
</div>
'; }
?>
Each of the taxonomies showing above has an image uploaded to it with custom fields (advanced custom fields). How do i show a custom field (product_type_image) in each of the div that gets generated? like this
Chocolates [product_type_image]
Marshmallows [product_type_image]
Popcorn [product_type_image]
..and so on...
i was trying out with
$productimage = get_field('product_type_image', $term->taxonomy.'_'.$term->term_id);
but it was unsuccessful to try to show anything
add this line
echo '<img src="' . get_field('product_type_image', $term->taxonomy . '_' . $term->term_id) . '"/>';
so it should look like this
<?php
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
if ($term->parent == 0) {
$terms = get_terms( 'product-type', 'parent='.$term->term_id ); }
else { $terms = get_terms( 'product-type', 'parent='.$term->parent ); }
foreach($terms as $term) {
echo '<div class="snack_type">' . $term->name . '</div>';
echo '<img src="' . get_field('product_type_img', $term->taxonomy . '_' . $term->term_id) . '"/>';
}
?>