I feel like this should be very simple but I've been searching for 15 mins and can't find it anywhere:
I have a loop that lists products in a certain category, and I need to modify it so that the picture and title are a link to the individual product page. Something like this is what I'm looking for.
<a href="<?php echo woocommerce_get_the_permalink() ?>">
This is what I have:
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 4, 'product_cat' => 'power-chairs', 'orderby' => 'ID' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<div class="product">
<div class="padding">
<?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder"/>'; ?>
<p class="caption"><?php the_title(); ?></p>
</div><!-- padding -->
</div><!-- product -->
<?php endwhile; ?>
Edit: Found it - it was href="get_permalink($product_id)"
Have you tried adding the a href link around the image and the title:
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 4, 'product_cat' => 'power-chairs', 'orderby' => 'ID' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<div class="product">
<div class="padding">
<a href="<?php echo woocommerce_get_the_permalink() ?>">
<?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder"/>'; ?></a>
<p class="caption"><?php the_title(); ?></p>
</div><!-- padding -->
</div><!-- product -->
Related
I'm trying to load latest product in Woocommerce (on home page). I want to exclude specific product category from display on the loop, but exclude product category id is not working for me.
What I have done so far:
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 12,
'post_status' => 'publish',
'taxonomy' => 'product_cat',
'exclude' => 29,//exclude mention category id from loop
'parent' => 0
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
?>
<div class="popular-inner">
<a href="<?php echo get_permalink(); ?>">
<div class="popular-image d-flex ">
<div class="align-self-center mx-auto ">
<?php the_post_thumbnail(); ?>
</div>
</div>
<div class="popular-content-wp">
<div class="popular-title">
<h6><?php echo get_the_title(); ?></h6>
</div>
<div class="popular-price">
<p><?php echo wc_price($product->get_price()); ?></p>
</div>
<div class="popular-add-to-cart">
<ul>
<li>
Add to Cart
</li>
</ul>
</div>
</div>
</a>
</div>
<?php endwhile; wp_reset_query();?>
Since wordpress version 3.1 using a taxonomy slug in a WP_Query is deprecated in flavor of tax_query. Also there are some other mistakes. See WP_Query taxonomy parameters.
Your revisited code:
<?php
$loop = new WP_Query( array(
'post_type' => 'product',
'posts_per_page' => 12,
'post_status' => 'publish',
'parent' => 0,
'tax_query' => array( array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => array( 29 ), // Term ids to be excluded
'operator' => 'NOT IN' // Excluding terms
) ),
) );
if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post();
// Get the instance of the WC_Product from the post ID
$product = wc_get_product( get_the_id() );
?>
<div class="popular-inner">
<a href="<?php echo get_permalink(); ?>">
<div class="popular-image d-flex ">
<div class="align-self-center mx-auto ">
<?php the_post_thumbnail(); ?>
</div>
</div>
<div class="popular-content-wp">
<div class="popular-title">
<h6><?php echo get_the_title(); ?></h6>
</div>
<div class="popular-price">
<p><?php echo wc_price( $product->get_price() ); ?></p>
</div>
<div class="popular-add-to-cart">
<ul>
<li>
Add to Cart
</li>
</ul>
</div>
</div>
</a>
</div>
<?php
endwhile;
wp_reset_postdata();
else: ?>
<p><?php _e( 'No posts found.' ); ?></p>
<?php endif;
?>
It should work now.
Newbie here,
I'm trying to display the list of my post depends on the category. But it doesn't display my post. I tried the different type of array, but no luck. I named my page taxonomy-blog_category.php
I call the page via site.com/blog_category/category
here's my current code and I know I'm so close but can't figure it out.
Here is the array:
<div class="row">
<?php $ctr = 1;
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$custom_args = array(
'post_type' => 'blog_post',
'orderby' => 'date',
'order' => 'DESC',
'post_status' => 'publish',
'posts_per_page' => 6,
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'blog-category',
'field' => 'slug',
'terms' => array('business','people','technology'),
),
),
);
Here is how I display the post
$custom_query = new WP_Query( $custom_args ); ?>
<?php if ( $custom_query->have_posts() ) : ?>
<?php while ( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
<div class="col-md-4 text-center">
<div class="content-container">
<div class="wrap">
<figure class="tint t2">
<img src="<?php echo wp_get_attachment_url( get_post_thumbnail_id() ); ?>" width="317px" height="240">
</figure>
</div>
<h2><?php the_title(); ?></h2>
<h3>By <?php the_field('author'); ?> | <span><?php echo get_the_date(); ?></span></h3>
<?php $content = get_field('content'); echo mb_strimwidth($content, 0, 200, '...');?>
<div class="read-more-btn">
read more
</div>
</div>
</div>
<?php $ctr++; endwhile; ?>
I Don't know if this is necessary but here's my code for pagination:
<div class="pagination-holder">
<ul class="pagination">
<?php
if (function_exists(custom_pagination)) {
custom_pagination($custom_query->max_num_pages,"",$paged);
}
?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</ul>
</div>
In Woocommerce, I Need some help with this custom product loop, in my code my result is : how it looks like
The loop doesn't stop and it is looping the same products for three or four times.
The code I am using is here :
<div class="container">
<div id="default_products_page_container" class="wrap wpsc_container">
<?php
remove_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 30 );
remove_action( 'woocommerce_before_shop_loop', 'woocommerce_result_count', 20 );
$cat = get_query_var( 'product_cat' );
$args = array( 'post_type' => 'product', 'posts_per_page' => 5, 'product_cat' => $cat, 'orderby' => 'rand' );
$loop = new WP_Query( $args );
?>
<div class="wpsc_default_product_list">
<?php
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<div class="col-lg-4 col-md-4 col-sm-12 col-xs-12">
<div class="default_product_display product_view_<?php echo get_permalink( $loop->post->ID ); ?>group">
<a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>"/>
<?php if (has_post_thumbnail( $loop->post->ID ))
{
?>
<div class="product-image-thumb">
<img src="<?php echo the_post_thumbnail_url( $loop->post->ID );?>"/>
</div>
<?php
} else
{?>
<div class="product-image-thumb">
<a>
<img src="<?php echo woocommerce_placeholder_img_src();?>"/>
</a>
</div>
<?php } ?>
</div>
</div>
<?php endwhile; ?>
</div>
<?php wp_reset_query(); ?>
</div>
</div>
I Hope this code gonna help you guys :)
<ul class="products">
<?php
$args = array(
'product_cat' => 'Shampoo',
'posts_per_page' => 4,
'orderby' => 'rand'
);
$loop = new WP_Query($args);
while ($loop->have_posts()) : $loop->the_post();
global $product; ?>
<div class="row">
<!-- <h2>Shampoo</h2> -->
<li class="product">
<a href="<?php echo get_permalink($loop->post->ID) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">
<?php woocommerce_show_product_sale_flash($post, $product); ?>
<?php if (has_post_thumbnail($loop->post->ID)) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog');
else echo '<img src="' . woocommerce_placeholder_img_src() . '" alt="Placeholder" width="300px" height="300px" />'; ?>
<h3><?php the_title(); ?></h3>
<span class="price"><?php echo $product->get_price_html(); ?></span>
</a>
<?php woocommerce_template_loop_add_to_cart($loop->post, $product); ?>
</li>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</ul>
<!--/.products-->
You need to do a tax query instead this way:
$loop = new WP_Query( array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => 5,
'tax_query' => array( array(
'taxonomy' => 'product_cat',
'field' => 'slug', // Or 'term_id' or 'name'
'terms' => get_query_var( 'product_cat' ), // A slug term
// 'include_children' => false // or true (optional)
)),
'orderby' => 'rand'
) );
Tested and works on Woocommerce product category archive pages…
Your code should work, although it will take all products in the loop (including those marked as "draft" when you are logged in). You can define only published by adding 'post_status' => 'publish' to your argument variable.
And you should take a look at those anchor tags (specifically the close part)
I've got some PHP code which I'm using in a WordPress website. Here's the code:
<h3>Case Studies</h3>
<?php
$the_query = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => -1,
'cat' => 3,
'meta_key' => 'sector',
'orderby' => 'meta_value',
'order' => 'ASC'
));
if( $the_query->have_posts() ):
while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<a href="<?php echo get_permalink(); ?>">
<h1><?php the_field('client_name'); ?></h1><p><?php the_field('sector'); ?></p>
<span style="background-image:url(<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); echo $url; ?>)"></span>
</a>
<?php endwhile; endif; ?>
<h3>Other Clients</h3>
<?php if( have_rows('clients') ):
while ( have_rows('clients') ) : the_row(); ?>
<a>
<h1><?php the_sub_field('client'); ?></h1><p><?php the_sub_field('sector'); ?></p>
<span></span>
</a>
<?php endwhile; endif; ?>
So - at the top, we've got "Case Studies" and this is just pulling in some details from the posts on the website (the client name and the sector).
Next, I've got "Other Clients" - this is set up as an Advanced Custom Field on the page that this code appears on. Pretty simple as well.
Now, here's the fun:
It works if I reverse the two sections ("Other Clients" first) but not this way - any ideas what's going wrong? I'm presuming it's something in the "Case Studies" section that's messing up the following one, but I'm at a loss. If I can provide any more info, let me know!
Much thanks in advance x
After finishing your custom query you must call wp_reset_postdata();
$the_query = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => -1,
'cat' => 3,
'meta_key' => 'sector',
'orderby' => 'meta_value',
'order' => 'ASC'
));
if( $the_query->have_posts() ):
while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<a href="<?php echo get_permalink(); ?>">
<h1><?php the_field('client_name'); ?></h1><p><?php the_field('sector'); ?></p>
<span style="background-image:url(<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); echo $url; ?>)"></span>
</a>
<?php endwhile; endif; ?>
<?php wp_reset_postdata(); ?>
Because your custom query $the_query->the_post(); overrides global $post object, after finishing your query must always do this.
I have a problems using wordpress. I'm editing wordpress page online and using array in it, so when I wrote this on my page,
<section id="recent">
<h1>Recently Added</h1>
<ul class="row-fluid">
<?php
$args = array( 'post_type' => 'product', 'stock' => 1, 'posts_per_page' => 4, 'orderby' =>'date','order' => 'DESC' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<li class="span3">
<a id="id-<?php the_id(); ?>" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="65px" height="115px" />'; ?>
<h3><?php the_title(); ?></h3>
<span class="price"><?php echo $product->get_price_html(); ?></span>
</a>
<?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>
</li><!-- /span3 -->
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</ul><!-- /row-fluid -->
</section><!-- /recent -->
The result, is the page is showing this instead what I wished for,
‘product’, ‘stock’ => 1, ‘posts_per_page’ => 4, ‘orderby’
=>’date’,’order’ => ‘DESC’ ); $loop = new WP_Query( $args ); while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?> ‘; ?>
get_price_html(); ?>
post, $product ); ?>
Please help me understand what is happening, and how to solve it.
At least I know that in this part of the codes,
$args = array( 'post_type' => 'product', 'stock' => 1, 'posts_per_page' => 4, 'orderby' =>'date','order' => 'DESC' );
The first '>' symbol, close the php tag
I think you should use curly braces for the if/else clause in this line:
<?php if (has_post_thumbnail( $loop->post->ID )) { echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); } else { echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="65px" height="115px" />'; } ?>