I want to display a specific product attribute on my product page in WooCommerce.
But I want to show not only the name but some extra information like a description and an image.
At the moment I'm using wc_get_product_terms for that:
<?php
global $product;
$values = wc_get_product_terms( $product->id, 'pa_attribute', array( 'fields' => 'all' ) ); if( $values ) : ?>
<div class="woocommerce-product-details>
<div class="content">
<ul class="list-unstyled">
<?php foreach ( $values as $term ) : ?>
<li class="">
<?php $icon = get_field('pa_attribute_icon', 'pa_attribute_'.$term->term_id); if( !empty($icon) ): ?>
<div class="highlight-img"><img src="<?php echo $icon['url']; ?>" alt="<?php echo $icon['alt']; ?>" /></div>
<?php endif; ?>
<div class="highlight-label"><?php echo $term->name; ?></div>
</li>
<?php endforeach; ?>
</ul>
</div>
</div>
<?php endif; ?>
I have read, that wc_get_product_terms is deprecated. Is there a better/newer solution to display the attributes with the additional informations?
wc_get_product_terms is not deprecated. woocommerce_get_product_terms it is.
Related
I am displaying product images on product page using ACF. Only permalink and title is displaying and product pictures are not displaying instead of that broken thumbnails are displaying. My field type and return format is post object and here is my code:
<div class="col-md-12">
<?php
$featured_posts = get_field('products_images');
if( $featured_posts ): ?>
<ul>
<?php foreach( $featured_posts as $featured_post ):
$permalink = get_permalink( $featured_post);
$title = get_the_title( $featured_post);
$image = get_field('products_images');
?>
<li>
<?php
if ( !empty($image) ); ?>
<img src="<?php echo $image['url']; ?>" />
<?php echo esc_html( $title ); ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>
I have a custom post type called projects and a custom taxonomy attached to that called sectors. I have also added the ability to add an image to each taxonomy using Advanced Custom Fields.
I'm displaying a list of the sectors on the homepage of my site which consists of the title of the taxonomy along with its image here:
So far, so good...
I'm struggling with surrounding either the image or title with the permalink for the taxonomy. I'd like to be able to click on it and it takes me to a page showing all of the projects inside that sector.
My loop is as follows:
<div class="container">
<div class="row no-gutters">
<?php
// loop through terms of the Sectors Taxonomy
$getArgs = array(
'parent' => 0,
'order' => 'DESC',
'orderby' => 'count',
'hide_empty' => false,
);
// get the taxonomy we are going to loop through.
$taxonomy = get_terms('sectors', $getArgs);
$terms = get_terms($taxonomy);
// Start the loop through the terms
foreach ($taxonomy as $term) {
// Get acf field Name
$image = get_field('sector_image', $term );
$url = $image['url'];
$title = $image['title'];
$alt = $image['alt'];
// which size?
$size = 'large';
$thumb = $image['sizes'][ $size ];
?>
<div class="col-md-6">
<div class="sector-item-container" style="position: relative;">
<div class="box-overlay"><?php echo $term->name; ?></div><!-- box overlay -->
<a href="<?php the_permalink(); ?>">
<img src="<?php echo $thumb; ?>" alt="<?php echo $alt; ?>" title="<?php echo $title; ?>" />
</a>
</div>
</div>
<?php } // end foreach ?>
</div>
</div>
I've tried adding tags like this around the elements I want to be clickable:
<a href="<?php the_permalink(); ?>">
</a>
or
<a href="<?php echo get_term_link($term->slug, $taxonomy); ?>">
</a>
But they don't seem to pull the links through...
Can anyone see what I'm doing wrong? I appreciate any insight at all :)
***** EDIT ***************
This seems to show a list of the taxonomies and also apply the link to them... so it's somehow a mix of both I need I think, this following code works but without the images!...
<?php
$taxonomy = 'sectors';
$terms = get_terms($taxonomy); // Get all terms of a taxonomy
if ( $terms && !is_wp_error( $terms ) ) :
?>
<div class="container-flex">
<div class="row no-gutters">
<?php foreach ( $terms as $term ) { ?>
<div class="col-md-6">
<div class="sector-item-container" style="position: relative;">
<a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>">
<div>
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail('page-thumb-mine');
}
?>
<?php
$image = get_field('sector_image');
if( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>
</div>
</a>
<div class="sector-item-title">
<a href="<?php echo get_term_link($term->slug, $taxonomy); ?>">
<h4><?php echo $term->name; ?></h4>
</a>
</div>
</div>
</div>
<?php } ?>
</div>
</div>
<?php endif;?>
<?php wp_reset_postdata(); ?>
This is from the codex on https://developer.wordpress.org/reference/functions/get_term_link/.
$terms = get_terms( 'species' );
echo '<ul>';
foreach ( $terms as $term ) {
// The $term is an object, so we don't need to specify the $taxonomy.
$term_link = get_term_link( $term );
// If there was an error, continue to the next term.
if ( is_wp_error( $term_link ) ) {
continue;
}
// We successfully got a link. Print it out.
echo '<li>' . $term->name . '</li>';
}
echo '</ul>';
For your specific case, try swapping the above with this: $terms = get_terms( 'sectors' );
Edit:
As for your images, retrieve them with get_field('sector_image', $id_of_term_or_post_or_whatever); Be sure to echo it.
$image = get_field('sector_image', id_of_term_or_post_or_whatever);
echo $image; //this might be $image['url'] or whatever, depending on how you set up ACF
Ok, I think I actually managed to combine the two bits of code like this and it seems to work!
<?php
$taxonomy = 'sectors';
$terms = get_terms($taxonomy); // Get all terms of a taxonomy
if ( $terms && !is_wp_error( $terms ) ) :
?>
<div class="container-flex">
<div class="row no-gutters">
<?php foreach ( $terms as $term ) {
$image = get_field('sector_image', $term );
$url = $image['url'];
$title = $image['title'];
$alt = $image['alt'];
$size = 'large';
$thumb = $image['sizes'][ $size ];
?>
<div class="col-md-6">
<div class="sector-item-container" style="position: relative;">
<a href="<?php echo get_term_link($term->slug, $taxonomy); ?>">
<div>
<img src="<?php echo $thumb; ?>" alt="<?php echo $alt; ?>" title="<?php echo $title; ?>" />
</div>
</a>
<div class="sector-item-title">
<a href="<?php echo get_term_link($term->slug, $taxonomy); ?>">
<h4><?php echo $term->name; ?></h4>
</a>
</div>
</div>
</div>
<?php } ?>
</div>
</div>
<?php endif;?>
<?php wp_reset_postdata(); ?>
First time trying to use Woocommerce booking to build a page.
I was changing the products to be bookable and they started to disappear from my custom loop using wc_get_products.
Product category, everything else is not changed. It is not getting bookable products.
Can I use another function for getting my bookable products?
I am very stuck, hoping someone can help.
Here is my code:
<ul>
<?php // Get some random products.
$args = array(
'category'=> $post->post_name,
'showposts'=>-1,
'type'=>'any'
);
$products = wc_get_products( $args );
$firstrow=true;
?>
<?php foreach ( $products as $item_id => $item_obj ) {?>
<?php
$item_data = $item_obj->get_data();
$class = ($firstrow) ? 'first' : '' ;
?>
<?php $url = get_permalink( $item_data['id'] ) ;?>
<div class="row">
<a href="<?php echo $url; ?>">
<div class="link">
<div class="floatleft">
<div class="plane fontcolor relative"> <img src="<?php echo get_template_directory_uri()?>/images/lennuk.svg">
</div>
<div class="inlineblock date <?php echo $class ?>">
<?php
echo $item_obj->get_attribute( 'pa_ajavahemik' );?>
</div>
</div>
<div class="headtitle floatleft"><?php echo $item_data['name']?>
</div>
<div class="floatright buttonround">
<img src="<?php echo get_template_directory_uri()?>/images/vali_viimase_reis.svg">
</div>
<div class="floatright tripprice green">
<?php echo $item_data['regular_price'].' €';?><span class="sale"><?php echo $item_data['sale_price']; ?></span>
</div>
<div class="floatright duration">
<?php
echo $item_obj->get_attribute( 'pa_kestus' );?>
</div>
<div class="clear"></div>
</div>
</a>
</div>
<?php
$firstrow=false;
}
?>
</ul>
I added added 'type' =>'booking' in the $args and it just worked for me.
I have created a Custom Post Type product and for this CPT I've also created a taxonomy with the name products_types.
Now on my overview page of all the products I would like to echo out the product type that was given to the product. But I keep getting bool(false).
My code:
<div class="row">
<?php
$loop = new WP_Query( array( 'post_type' => 'product') );
if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div class="col-md-4 col-lg-3 work">
<div class="category">
<?php
$category = get_the_terms('product', 'products_types');
var_dump($category);
echo $category;
?>
</div>
<a href="<?php the_permalink() ?>" class="work-box"> <img src="<?= get_field('image'); ?>" alt="">
<div class="overlay">
<div class="overlay-caption">
<p><?php echo the_title() ?></p>
</div>
</div>
</a>
</div>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
</div>
Anyone can help me out here please?
You need to pass the Post ID or object in first parameter of get_the_terms(). Used get_the_ID() which return the post ID.
Example:
foreach (get_the_terms(get_the_ID(), 'products_types') as $cat) {
echo $cat->name;
}
How to print taxonomy terms of custom post type in WordPress loop?
<div class="row">
<?php
$loop = new WP_Query( array( 'post_type' => 'product') );
if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div class="col-md-4 col-lg-3 work">
<div class="category">
<?php
$terms = get_the_terms( get_the_ID(), 'products_types' );
if ( $terms && ! is_wp_error( $terms ) ) :
$category_links = array();
foreach ( $terms as $term ) {
$category_links[] = $term->name;
}
$categories = join( ", ", $category_links );
?>
<?php printf( esc_html( $categories ) ); ?>
<?php endif; ?>
</div>
<a href="<?php the_permalink() ?>" class="work-box"> <img src="<?= get_field('image'); ?>" alt="">
<div class="overlay">
<div class="overlay-caption">
<p><?php echo the_title() ?></p>
</div>
</div>
</a>
</div>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
</div>
Seems that I am just too stupid to achieve this. Seems so simple, yet…
I created a template and try to display a custom portfolio (registered by a custom post plugIn) This works fine with the following code:
<div id="container">
<?php
//Define your custom post type name in the arguments
$args = array('post_type' => 'boxes_scientists');
//Define the loop based on arguments
$loop = new WP_Query( $args );
//Display the contents
while ( $loop->have_posts() ) : $loop->the_post();
?>
/*
<?php
foreach((get_the_category()) as $category) {
echo $category->cat_name . ' ';
}
?>
<?php the_category(', '); ?>
*/
<div class="some_base_class [categories of the post need to go here]">
<a class="element" href="<?php the_permalink(); ?>"></a>
<div class="portfolio-box">
<div class="portfolio-naming">
<h2 class="portfolio-title"><?php the_title(); ?></h2>
<h3 class="portfolio-attributes"><?php the_content(); ?></h3>
</div>
</div>
<?php the_post_thumbnail(); ?>
</div>
<?php endwhile;?>
but I can’t get to work the commented out code part and display the categories inside the class tag of my container element (for each post displayed in the loop).
I actually found also this concept:
https://lorelle.wordpress.com/2007/09/06/using-wordpress-categories-to-style-posts
which seemed to be exactly what I need but sadly this didn’t work at all for me. (placed the function inside functions.php on my child theme and theme both without any result)
What I am misunderstanding here? Can someone show me the correct code I have to use? Would be so awesome. Thanks in advance!
EDIT
So this finally brings me on the right way:
<?php
[…]
$category = get_the_category();
$firstCategory = $category[0]->cat_name;?>
<div class="some_base_class <?php echo $firstCategory ?>">
I missed the “echo” thing inside my div. Now I will have to find the way to display all categories of my post instead of only the first.
EDIT 2:
<div id="boxes_section" class="main-content master-section-content nano_boxes no-detect no-padding">
<div class="container">
<div class="row">
<div class="col-md-12 normal-column start-animated-content az-fade-in" data-delay="300">
<div class="blank-divider" style="height: 30px;"></div>
<div id="portfolio-item-section" class="portfolio-output masonry-ly-portfolio classic-module no-pagination" data-cols="3">
<?php
//Define your custom post type name in the arguments
$args = array('post_type' => 'boxes_scientists');
//Define the loop based on arguments
$loop = new WP_Query( $args );
//Display the contents
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div class="single-portfolio-item az-col-full-width-4 [NEED THE CLASSES HERE]">
<a class="classic-portfolio-box normal-type-prt" href="<?php the_permalink(); ?>">
<p class="site_leave"><i class="font-icon-forward"></i>You are going to leave this website</p>
</a>
<div class="portfolio-box">
<div class="portfolio-naming">
<h2 class="portfolio-title"><?php the_title(); ?></h2>
<h3 class="portfolio-attributes"><?php the_content(); ?></h3>
</div>
</div>
<?php the_post_thumbnail(); ?>
</div>
<?php endwhile;?>
</div>
</div>
</div>
</div>
</div>
I just write code category at functions.php like this:
function sps_category(){
$categories = get_the_category();
foreach ( $categories as $category ) {
echo '<a href="'.esc_url( get_category_link( $category->term_id ) ).'">
'.esc_html( $category->cat_name ).'
</a>';
}
}
and i call my functions at my page
<?php sps_category() ?>
but in other ways,
you can write your code in class like this:
$categories = get_the_category();
foreach ( $categories as $category ) {
echo '<div class=".esc_attr($category->cat_name)."><a href="'.esc_url( get_category_link( $category->term_id ) ).'">
'.esc_html( $category->cat_name ).'
</a><div>';
}
It can editable and dynamic if you want to show just 1 category.
And dont forget to use escape function if you put some php variabel/functions if you write in attribut html
for example use esc_attr(somecode) if it class/title/name/id attribute.