i wanted to add recent woocommerce product in home page.
<?php
$args = array( 'post_type' => 'product', 'stock' => 1, 'posts_per_page' => 1, 'orderby' =>'date','order' => 'DESC' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product;
?>
Content Here
<?php endwhile;
wp_reset_query(); ?>
i wanted to recent product featured image in woocommerce which code i write to get image.
Try below code:
<ul>
<?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" />';
?>
</a>
<?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>
</li><!-- /span3 -->
<?php endwhile; ?>
<?php wp_reset_query(); ?>
This might help you :
$args = array(
'post_type' => 'product',
'posts_per_page' => 10,
'orderby' => 'date',
'fields' => 'ids',
'order' => 'DESC');
$newly_arr = query_posts($args);
$all_images = array();
foreach ($newly_arr as $a){
$post_thumbnail_id = get_post_thumbnail_id($a);
$all_images[] = wp_get_attachment_url($post_thumbnail_id);
}
echo '<pre>';
print_r($all_images);
die;
Related
I am currently putting this array into my table of contents. To create something like this...
<?php
$args = array(
'post_category' => 'live',
'post_status' => 'publish',
‘order’ => ‘ASC’,
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
print the_field('venue');
the_excerpt();
endwhile;
wp_reset_postdata();
?>
When I add a new custom post the information is displaying on the same line.
I want each post to start on a new line.
Use tag div for record in 1 one.
<?php
$args = [
'post_category' => 'live',
'post_status' => 'publish',
'order' => 'ASC',
];
$loop = new WP_Query( $args );
$count = 1;
while ( $loop->have_posts() ) : $loop->the_post();
echo "<div class='item item-".$count."'>";
echo the_field( 'venue' );
the_excerpt();
echo "</div>";
$count++;
endwhile;
wp_reset_postdata();
?>
<?php
$args = [
'post_category' => 'live',
'post_status' => 'publish',
'order' => 'ASC',
];
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo the_field( 'venue' );
the_excerpt();
echo PHP_EOL;
endwhile;
wp_reset_postdata();
?>
In case of HTML
<?php
$args = [
'post_category' => 'live',
'post_status' => 'publish',
'order' => 'ASC',
];
$loop = new WP_Query( $args );
?>
<div class="list">
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="list-item">
<?php echo the_field( 'venue' ); ?>
<?php the_excerpt(); ?>
</div>
<?php endwhile; ?>
</div>
<?php wp_reset_postdata(); ?>
I list my categories in a page using the following code:
<?php
$terms = get_terms( array(
'taxonomy' => 'category',
'hide_empty' => true,
) );
$count = count($terms);
$categories = array();
if ($count > 0) :
foreach ($terms as $term) {
$args = array(
'post_type' => 'post',
'posts_per_page' => 1,
'show_count' => 1,
'orderby' => 'rand',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'categorys',
'field' => 'slug',
'terms' => $term->slug
)
)
);
$post_from_category = new WP_Query( $args );
if( $post_from_category->have_posts() ){
$post_from_category->the_post();
}else{}
$term->slug;
$term->name; ?>
<article id="post-<?php the_ID(); ?>" <?php post_class('thumb-block'); ?>>
<a href="<?php echo bloginfo('url'); ?>?categories=<?php echo $term->slug; ?>" title="<?php echo $term->name; ?>">
<header class="entry-header">
<span class="category-title"><?php echo $term->name; ?></span>
</header><!-- .entry-header -->
</a>
</article><!-- #post-## -->
<?php }
endif; ?>
My question is: How can I list the categories which contains minimum 5 posts only?
Thank you very much!
Inside your foreach loop you can just check to make sure $term->count is greater than or equal to 5. There's also a few other things I noticed:
You define a $categories array but don't appear to use it again.
Try not to mix if: endif; and if{} statements - stick to a syntax for ease-of-maintenance.
You can just check to see if $terms gets set to a truthy value instead of checking its count.
Are you sure your Tax Query is set properly? It's checking the taxonomy "categorys" which is a typo of "categories"
You don't need an empty else{} loop after your ->have_posts() check
You don't need to instantiate the $term object variables before the article.
You should consider using home_url() instead of bloginfo('url')
All that said, this should get you started:
<?php
$term_args = array(
'taxonomy' => 'category',
'hide_empty' => true,
);
if( $terms = get_terms( $term_args ) ){
foreach( $terms as $term ){
if( $term->count >= 5 ){
$args = array(
'post_type' => 'post',
'posts_per_page' => 1,
'show_count' => 1,
'orderby' => 'rand',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'categorys',
'field' => 'slug',
'terms' => $term->slug
)
)
);
$post_from_category = new WP_Query( $args );
if( $post_from_category->have_posts() ){ $post_from_category->the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class( 'thumb-block' ); ?>>
<a href="<?= home_url( "?categories={$term->slug}" ); ?>" title="<?= $term->name; ?>">
<header class="entry-header">
<span class="category-title"><?= $term->name; ?></span>
</header>
</a>
</article>
<?php }
}
}
}
?>
IN my code I am fetching data behalf of category id .but problem is that <div id="<?php echo $term_id = $term->term_id;?>" class="tabcontent">. $term_id does not change in this div.Here is my code .
<?php
//loop the names of the slugs for the portfolio_categories
$terms = get_terms( array ( 'taxonomy' => 'portfolio_categories', 'hide_empty' => false, 'parent' => 0, 'orderby' => 'date', 'order' => 'DESC' ));
foreach ($terms as $term) {
$slug= $term->slug;
$term_id = $term->term_id;
$args = array(
'post_type' => 'Portfolio',
'tax_query' => array(
array(
'taxonomy' => 'portfolio_categories',
'terms' => $slug,
'field' => 'slug',
)
),
'orderby' => 'ID',
'order' => 'DESC',
'posts_per_page' => -1
);
}
?>
<?php
$posts_query = new WP_Query( $args );
if (have_posts()) :?>
<div id="<?php echo $term_id = $term->term_id;?>" class="tabcontent">
<?php
while ( $posts_query->have_posts() ) : $posts_query->the_post();?>
<?php echo '<div class="col-1-3">';?>
<?php echo $term_id = $term->term_id;?>
<?php $thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), "size" );?>
<div class="wrap-col" >
<a class="fancybox" href="<?php echo $thumbnail[0]; ?>" data-fancybox-group= gallery>
<img src="<?php echo $thumbnail[0];?>"></a> </div>
</div>
<?php echo '</div>';?>
<?phpendwhile;endif;?>
</div>
Hi every one I have solved this issue .problem was only .div was not manage in under for loop. so this is answer if you trying to get custom post by custom category.
<?php
$taxonomy = 'portfolio_categories';
$args = array(
'hide_empty' => false
);
global $terms;
global $terms;
$terms = get_terms( 'portfolio_categories', $args );
?>
<div class="tab">
<?php $i = 0; ?>
<?php foreach ( $terms as $term ) { ?>
<?php $i++; ?>
<button class="tablinks" onclick="openCity(event, '<?php echo $term->term_id;?>')"
<?php if ($i == 1) { echo 'id="defaultOpen"'; } ?> ><?php echo $term->name;?></button>
<?php } ?>
</div>
<?php
//loop the names of the slugs for the portfolio_categories
$terms = get_terms(array('taxonomy' => 'portfolio_categories', 'hide_empty' => false, 'parent' => 0, 'orderby' => 'date', 'order' => 'DESC'));
foreach ($terms as $term) {
$slug = $term->slug;
$term_id = $term->term_id;
$args = array(
'post_type' => 'Portfolio',
'tax_query' => array(
array(
'taxonomy' => 'portfolio_categories',
'terms' => $slug,
'field' => 'slug',
)
),
'orderby' => 'ID',
'order' => 'DESC',
'posts_per_page' => -1
);
//}//Instead closing of here
?>
<?php
$posts_query = new WP_Query($args);
if (have_posts()) :
?>
<div id="<?php echo $term_id = $term->term_id; ?>" class="tabcontent">
<?php while ($posts_query->have_posts()) : $posts_query->the_post(); ?>
<?php echo '<div class="col-1-3">'; ?>
<?php echo $term_id = $term->term_id; ?>
<?php $thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), "size"); ?>
<div class="wrap-col" >
<a class="fancybox" href="<?php echo $thumbnail[0]; ?>" data-fancybox-group= gallery>
<img src="<?php echo $thumbnail[0]; ?>"></a> </div>
<?php echo '</div>'; ?>
<?php
endwhile;
endif;?>
</div>
<?php }//Close foreach here
?>
Inorder to get every term->term_id close the foreach at the end
//loop the names of the slugs for the portfolio_categories
$terms = get_terms(array('taxonomy' => 'portfolio_categories', 'hide_empty' => false, 'parent' => 0, 'orderby' => 'date', 'order' => 'DESC'));
foreach ($terms as $term) {
$slug = $term->slug;
$term_id = $term->term_id;
$args = array(
'post_type' => 'Portfolio',
'tax_query' => array(
array(
'taxonomy' => 'portfolio_categories',
'terms' => $slug,
'field' => 'slug',
)
),
'orderby' => 'ID',
'order' => 'DESC',
'posts_per_page' => -1
);
//}//Instead closing of here
?>
<?php
$posts_query = new WP_Query($args);
if (have_posts()) :
?>
<div id="<?php echo $term_id = $term->term_id; ?>" class="tabcontent">
<?php while ($posts_query->have_posts()) : $posts_query->the_post(); ?>
<?php echo '<div class="col-1-3">'; ?>
<?php echo $term_id = $term->term_id; ?>
<?php $thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), "size"); ?>
<div class="wrap-col" >
<a class="fancybox" href="<?php echo $thumbnail[0]; ?>" data-fancybox-group= gallery>
<img src="<?php echo $thumbnail[0]; ?>"></a> </div>
</div>
<?php echo '</div>'; ?>
<?php
endwhile;
endif;
}//Close foreach here
?>
</div>
I'm setting up a featured products area on my website on the homepage, and it works fine at the moment but I'd also like to make the images clickable and once clicked I want them to go to the product page it self. Also, I'd like the woocommerce_template_loop_add_to_cart to also go to the product page after adding to the cart.
My code:
<div class="featured-products">
<h1 class="featured-products">Featured Products</h1>
<?php
$meta_query = WC()->query->get_meta_query();
$meta_query[] = array(
'key' => '_featured',
'value' => 'yes'
);
$args = array(
'post_type' => 'product',
'stock' => 1,
'showposts' => 4,
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => $meta_query
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<li>
<?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() . ' />';
?>
<p><?php the_title(); ?></p>
<p><?php echo $product->get_price_html(); ?></p>
<?php
woocommerce_template_loop_add_to_cart( $loop->post, $product );
?>
</li>
<?php
endwhile;
wp_reset_query();
?>
Thanks!
change this
echo get_the_post_thumbnail( $loop->post->ID, 'shop_catalog' );
to this
echo sprintf('%s', get_the_permalink( $loop->post->ID ), get_the_post_thumbnail( $loop->post->ID, 'shop_catalog' ) );
I'm developing themes for woocommerce, I need help to retrieve information from products by categories, example,
I want to display products from 'Shirt' Categories limit by 3 items, here's the code from woo themes that show products by featured products,( i tried to change to display by categories and not work)
<ul class="featured-products products">
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 6, 'meta_query' => array( array('key' => '_visibility','value' => array('catalog', 'visible'),'compare' => 'IN'),array('key' => '_featured','value' => 'yes')) );
$i = 0;
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); $_product; $i++;
if ( function_exists( 'get_product' ) ) {
$_product = get_product( $loop->post->ID );
} else {
$_product = new WC_Product( $loop->post->ID );
}
?>
<li class="product <?php if ($i%3==0) echo ' last'; if (($i-1)%3==0) echo ' first'; ?>">
<div class="inner">
<?php woocommerce_show_product_sale_flash( $post, $_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 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" />'; ?>
<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 ); ?>
<?php smpl_product_more_details(); ?>
</div>
</li>
<?php endwhile; ?>
</ul>
I'm new for this,
Thanks in advance
To get featured product by a specific category you just need to use wc_get_products with featured set to true and the category you specify. See below code.
<?php
// Get featured products by category. on this case its "shirts" which is the slug of the category.
$query_args = array(
'featured' => true,
'category' => array( 'shirts' ),
);
$products = wc_get_products( $query_args );
You can see the full tutorial here https://jameshwartlopez.com/plugin/get-featured-products-of-a-category/
$prod_categories = array(10, 27);
$product_args = array(
'numberposts' => $limit,
'post_status' => array('publish', 'pending', 'private', 'draft'),
'post_type' => array('product', 'product_variation'),
'orderby' => 'ID',
'suppress_filters' => false,
'order' => 'ASC',
'offset' => 0
);
if (!empty($prod_categories)) {
$product_args['tax_query'] = array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $prod_categories,
'operator' => 'IN',
));
}
$products = get_posts($product_args);
Replace the $args with the following code
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'terms' => array_map( 'sanitize_title', explode( ',', 'ENTER_CATEGORY' ) ),
'field' => 'slug',
'operator' => $atts['operator']
)
)
);
You just need to replace the ENTER_CATEGORY word with the category name you want to display.
Let me know if this fulfills your requirements.
Please use below Query
<ul class="products">
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 1, 'product_cat' => 'shoes', 'orderby' => 'rand' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<h2>Shoes</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>
<?php endwhile; ?>
<?php wp_reset_query(); ?>