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) . '"/>';
}
?>
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 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 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
The problem is that I cannot display the category of a post:
http://screencast.com/t/hdQjpSV0Q
I have the following code in a function.php file:
// GET FEATURED IMAGE
function ST4_get_featured_image($post_ID) {
$custom_meta = get_post_custom(get_the_ID());
return $custom_meta["lumen_portfolio_preview_image_image"][0];
}
add_filter('manage_posts_columns', 'ST4_columns_head');
add_action('manage_posts_custom_column', ('ST4_columns_content'), 10, 2);
// ADD NEW COLUMN
function ST4_columns_head($defaults) {
$defaults['featured_image'] = 'Featured Image';
$defaults['categories_portfolio'] = 'Category';
return $defaults;
}
// SHOW THE FEATURED IMAGE
function ST4_columns_content($column_name, $post_ID) {
if ($column_name == 'featured_image') {
$post_featured_image = ST4_get_featured_image($post_ID);
if ($post_featured_image) {
echo '<img style="width:300px;height:200px;" src="' . $post_featured_image . '" />';
}
}
elseif ($column_name == 'categories_portfolio') {
$terms = get_the_terms( $post->ID , 'category' );
foreach ( $terms as $term ) {
$term_link = get_term_link( $term, 'category' );
if( is_wp_error( $term_link ) )
continue;
echo '' . $term->name . '';
}
}
}
I have empty results, NULL, but I want to have the post category field. I used many wordpress functions, but no success.
I've noticed that your using the $post->ID as your parameter in your get_the_terms function, anyways I assume that the problem is you can't retrieve the category of the post, I would recommend using the get_the_category function in place of the get_the_terms in your code:
// SHOW THE FEATURED IMAGE
function ST4_columns_content($column_name, $post_ID) {
if ($column_name == 'featured_image') {
$post_featured_image = ST4_get_featured_image($post_ID);
if ($post_featured_image) {
echo '<img style="width:300px;height:200px;" src="' . $post_featured_image . '" />';
}
}
elseif ($column_name == 'categories_portfolio') {
//replace this $terms = get_the_terms( $post->ID , 'category' );
$terms = get_the_category($post_ID); //should you be using this instead of $post->ID?
foreach ( $terms as $term ) {
//replace this $term_link = get_term_link( $term, 'category' );
$term_link = get_category_link($term->term_id);
if( is_wp_error( $term_link ) )
continue;
echo '' . $term->name . '';
}
}
}
You can do a var_dump($terms) to further see the values being returned by this function, hope it helps, cheers!
I think you are using custom post type "Portfolio" as shown in attached screenshot.
Use wp_get_post_terms rather than get_the_terms
Update following code:
$terms = get_the_terms( $post->ID , 'category' );
to
$terms = wp_get_post_terms( $post->ID , 'category', array("fields" => "all") );
Note that 2nd parameter is the taxonomy name you are using while registering taxonomy.
See an example below where 'testimonials-cat' is used to register taxonomy.
register_taxonomy('testimonials-cat', 'testimonials', $args);
Your code for above taxonomy will be:
$terms = wp_get_post_terms($post->id, 'testimonials-cat', array("fields" => "all"));