There is a cycle that outputs products
<?php
$args = array('post_type' =>
'product');
$loop = new WP_Query($args);
foreach ($loop->posts as $have_post) : ?>
<div class="swiper-slide">
<a href="
<?php
$permalink = get_post_permalink($have_post->ID);
echo $permalink;
?>
" class="goods__item">
<div class="goods__text">
<?php
echo $have_post->name; ?>
</div>
<div class="goods__img">
<?php
echo get_the_post_thumbnail($have_post->ID, 'full', array('alt' => $have_post->post_title, 'data-product_id' => $have_post->ID)); ?>
</div>
<div class="goods__name"><?php echo $have_post->post_title; ?></div>
<div class="goods__price">
<?php
$price = get_post_meta($have_post->ID, '_price', true);
echo $price; ?> руб.
</div>
</a>
</div>
<?php endforeach; ?>
Need to show the category of current product here. I cant get the category. Please help
<div class="goods__text"><?php echo $have_post->name; ?></div>
You could use get_the_terms to get the correct terms. I believe it's product_cat the correct taxonomy.
$terms = get_the_terms( $have_post->ID, 'product_cat' );
foreach ($terms as $term) {
$product_cat_id = $term->term_id;
$product_cat_name = get_cat_name($product_cat_id);
break;
}
Try get_the_category( $post->ID );
Related
On my wordpress site, I have a section only displaying a lidt of post excerpts from within a specific parent category. Each post has a single sub-category which I'd like to also display alongside the title and excerpt.
At the moment I'm able to get and siaply the required posts but each post is displaying every subcategory, even ones not assigned to it. How can I modify this to show only assigned subcategories?
<div class="blog-items">
<?php
$args = array( 'posts_per_page' => 8, 'category' => 1 );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post );
?>
<div class="blue">
<p><span>
<?php
$args2 = array('child_of' => 1);
$categories = get_categories( $args2 );
foreach($categories as $category) {
$the_sub = $category->name;
echo $the_sub;
} ?>
</span></p>
<h3><?php the_title(); ?></h3>
<p><?php the_excerpt(__('(more…)')); ?></p>
read more
</div>
<?php endforeach;
wp_reset_postdata();?>
</div>
EDIT:
With Growdzens help I ended up with this that worked.
<div class="blog-items">
<?php
$args = array( 'posts_per_page' => 8, 'category' => 1 );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post );
?>
<div class="blue">
<p>
<span>
<?php
$categories = get_the_category( $post->ID);
foreach($categories as $category) {
$the_sub = $category->name;
$cat1 = 4;
$cat2 = $category->cat_ID;
if( $cat2 != 1 ){
echo $the_sub;
}
}
?>
</span>
</p>
<h3><?php the_title(); ?></h3>
<p><?php the_excerpt(__('(more…)')); ?></p>
read more
</div>
<?php endforeach;
wp_reset_postdata();?>
</div>
Wordpress has a function for getting all categories of a post on which you can read more here: get_the_category()
This function can be used in the loop like the example below to get the desired results.
<div class="blog-items">
<?php
$args = array( 'posts_per_page' => 8, 'category' => 1 );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post );
?>
<div class="blue">
<p><span>
<?php
$categories = get_the_category( $post->ID);
foreach($categories as $category) {
$the_sub = $category->name;
echo $the_sub;
} ?>
</span></p>
<h3><?php the_title(); ?></h3>
<p><?php the_excerpt(__('(more…)')); ?></p>
read more
</div>
<?php endforeach;
wp_reset_postdata();?>
</div>
Update:
To check if a category is a child of a specific other category you can use the Wordpress function cat_is_ancestor_of()
<div class="blog-items">
<?php
$args = array( 'posts_per_page' => 8, 'category' => 1 );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post );
?>
<div class="blue">
<p><span>
<?php
$categories = get_the_category( $post->ID);
$parent_id = 4; //change this ID to the parent ID you want to show the subcategories for
foreach($categories as $category) {
$the_sub = $category->name;
if(cat_is_ancestor_of( $parent_id, $category->cat_ID )){
echo $the_sub;
}
} ?>
</span></p>
<h3><?php the_title(); ?></h3>
<p><?php the_excerpt(__('(more…)')); ?></p>
read more
</div>
<?php endforeach;
wp_reset_postdata();?>
</div>
I am using this plugin
https://woocommerce.com/products/custom-product-tag-image/
To add images to my tags.
I'd like to display a list of tags with images. I've tried so many approaches but nothing yet.
Here's my code which outputs a list of tags but no images.
<?php $taxonomyName = "product_tag";
$terms = get_terms(array('taxonomy' => 'product_tag', 'hide_empty' => false)); ?>
<?php foreach ( $terms as $term ) { ?>
<div class="item" >
<?php
$thumb_id = get_woocommerce_term_meta( $term->term_id, 'thumbnail_id', true );
$term_img = wp_get_attachment_url( $thumb_id );
?>
<img src="<?php echo $term_img; ?>" />
<?php echo $term->name; ?>
</div>
<?php } ?>
Suggestions?
You can use WC get_woocommerce_term_meta() and WP wp_get_attachment_url(). check below code.
<?php foreach ( $terms as $term ) { ?>
<div class="item">
<?php
$thumb_id = get_woocommerce_term_meta( $term->term_id, 'thumbnail_id', true );
$term_img = wp_get_attachment_url( $thumb_id );
?>
<?php echo $term->name; ?>
</div>
<?php } ?>
The code below is what worked for me as woocommerce_get_term_meta is deprecated and thumbnail_id doesn't exist anymore:
<?php $taxonomyName = "product_tag";
$terms = get_terms(array('taxonomy' => 'product_tag', 'hide_empty' => false)); ?>
<?php foreach ( $terms as $term ) { ?>
<div class="item" >
<?php
$term_image_id = get_term_meta( $term->term_id, 'product_search_image_id', true );
$term_image = wp_get_attachment_url( $term_image_id );
?>
<img src="<?php echo $term_image; ?>" />
<?php echo $term->name; ?>
</div>
<?php } ?>
I need to echo the category slug for each post for the data-filter value. Can someone advise what I'm doing wrong?
<div id="container" class="isotope">
<?php
$args = array(
'post_type' => 'Photos',
);
$_posts = new WP_Query($args);
?>
<?php
if ( $_posts->have_posts() ) : while ( $_posts->have_posts() ) : $_posts->the_post(); ?>
<!-- Here is the issue -->
<div class="grid-item" data-filter="
<?php
$categories = get_the_category(get_the_id());
foreach ($categories as $category){
echo $category->slug.' ';}?>"
>
<a onClick='showDialog()' data-target="#lightbox">
<img src="<?php the_field('image'); ?>" alt="">
</a>
</div>
<?php
endwhile; endif;
?>
</div>
Please change code like this
<?php
$categories = get_the_category(get_the_ID());
foreach ($categories as $category){
$slug = $category->slug;
?>
<div class="grid-item" data-filter="<?php echo $slug; ?>">
<?php
}
?>
How can I get the custom post type category link in the while have_posts loop?
Here is the code I am using to get the category name:
<?php query_posts(array(
'post_type' => 'portfolio'
)); ?>
<?php while (have_posts()):
the_post(); ?>
<?php $feat_image = wp_get_attachment_url(get_post_thumbnail_id($post->ID)); ?>
<div class="col-md-3 col-md-3-portfolio">
<div class="portfoliobox">
<img src="<?php echo $feat_image; ?>" />
<div class="portfolio-title-block">
<h2><?php the_title(); ?></h2>
<?php
$terms = get_the_terms($post->ID, 'portfolio-category');
if ($terms && !is_wp_error($terms)):
$tslugs_arr = array();
foreach ($terms as $term)
{
$tslugs_arr[] = $term->slug;
}
$terms_slug_str = join(" ", $tslugs_arr);
endif;
$idObj = get_category_by_slug('portfolio-category');
$id = $idObj->term_id;
?>
<p><?php echo $terms_slug_str; ?></p>
</div>
</div>
</div>
<?php
endwhile; ?>
I'm currently playing around with WOOCOMMERCE V2.0.13 and I'm trying to display each product from the current product category (e.g. Construction Products when on the Construction Page), I've managed to display the single products from within the current category but if the product is also in another category (e.g Construction and Enviroment) then the current category breaks and shows zero products either from Construction or Enviroment.
If I could get some advice/help on displaying products from the current category and allow it to work with products that are in multiple categories I'd trully apreciate the help and time.
I'm more than happy to recode this entire section to make it work, here is my code below please let me know if I've missed anything.
Thank you.
<ul class="products">
<?php
global $post, $product;
$categ = $product - > get_categories();
$categ2 = preg_replace('/<a href=\"(.*?)\">(.*?)<\/a>/', "\\2", $categ);
?>
<?php
global $product;
$args = array('post_type' = > 'product', 'posts_per_page' = > '999', 'product_cat' = > $categ2, );
$loop = new WP_Query($args);
while ($loop - > have_posts()): $loop - > the_post();
global $product;
?>
<li>
<a href = "<?php echo get_permalink(); ?>">
<?php
if (has_post_thumbnail()) {
$image = get_the_post_thumbnail($post - > ID, apply_filters('single_product_large_thumbnail_size', 'shop_single'));
$image_title = esc_attr(get_the_title(get_post_thumbnail_id()));
$image_link = get_permalink($product_id);
$attachment_count = count($product - > get_gallery_attachment_ids());
echo apply_filters('woocommerce_single_product_image_html', sprintf('%s', $image_link, $image_title, $image), $post - > ID);
} else {
echo apply_filters('woocommerce_single_product_image_html', sprintf('<img src="%s" alt="Placeholder" />', woocommerce_placeholder_img_src()), $post - > ID);
} ?>
</a>
<div>
<h3>
<?php the_title();?>
<span>
<?php
if ($price_html = $product - > get_price_html()) {
?>
<span class = "price">
<?php echo $price_html; ?>
</span>
<?php } ?>
</span>
</h3>
</div>
<div>
<p>
<?php
$excerpt = get_the_excerpt();
echo string_limit_words($excerpt, 15);
?>
</p>
</div>
</li>
<?php endwhile; ?>
</ul>
<?php if ( have_posts() ) : ?>
<?php woocommerce_product_loop_start(); ?>
<?php woocommerce_product_subcategories(); ?>
<div class="courses-main">
<ul class="products">
<?php while ( have_posts() ) : the_post(); ?>
<li>
<a href="<?php echo get_permalink(); ?>">
<?php
if ( has_post_thumbnail() ) {
$image = get_the_post_thumbnail( $post->ID, apply_filters( 'single_product_large_thumbnail_size', 'shop_single' ) );
$image_title = esc_attr( get_the_title( get_post_thumbnail_id() ) );
$image_link = get_permalink( $product_id );
$attachment_count = count( $product->get_gallery_attachment_ids() );
echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '%s', $image_link, $image_title, $image ), $post->ID );
} else {
echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '<img src="%s" alt="Placeholder" />', woocommerce_placeholder_img_src() ), $post->ID );
}
?>
</a>
<div>
<h3>
<?php the_title();?>
<span>
<?php if ( $price_html = $product->get_price_html()) { ?>
<span class="price"><?php echo $price_html; ?></span>
<?php } ?>
</span>
</h3>
</div>
<div>
<p>
<?php
$excerpt = get_the_excerpt();
echo string_limit_words($excerpt,15);
?>
</p>
</div>
</li>
<?php endwhile;?>
</ul>
</div>
<?php woocommerce_product_loop_end(); ?>
<?php endif; ?>