I'm having some problems trying to show the first image of a gallery product on woocommerce,
I´m working on a personal wordpress theme, with personal template, all this work is into an owl carousel to show every product on a different slide,
I´m new with Woocommerce, I´m using WP_Query to show the products of a category, inside I´m showing the_title, the_content, the_permalink of every product on a while, I want to show the first and only one image inside the gallery of every product but I don´t know how to reach to it:
<div class="owl-carousel owl-theme" id="carousel">
<?php
$params = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => '-1',
'product_cat' => 'barras'
);
$wc_query = new WP_Query($params);
if ($wc_query->have_posts()) :
while ($wc_query->have_posts()) :
$wc_query->the_post();
?>
<div class="item">
<a href="<?php the_permalink();?>">
<img src="<?php echo get_template_directory_uri(); ?>/assets/img/prev.png" alt="">
<img src="<?php echo get_template_directory_uri(); ?>/assets/img/next.png" alt="">
<p class="titulo-producto-slider"><?php the_title(); ?></p>
</a>
<div class="espacio-10"></div>
<div class="descripcion-producto-slider">
<?php the_content(); ?>
</div>
<div class="ver-detalle">
<ul>
<li>
Ver detalles
</li>
</ul>
</div>
</div>
<?php
endwhile;
wp_reset_postdata();
else: ?>
<p><?php _e( 'No Products' );?></p>
<?php endif;
?>
</div>
Sorry if you don´t understand something, it is my first question on stack and I don´t speak English,
Thanks a lot,
Try this:
$product = new WC_Product( get_the_ID() );
$attachment_ids = $product->get_gallery_image_ids();
if ( is_array( $attachment_ids ) && !empty($attachment_ids) ) {
$first_image_url = wp_get_attachment_url( $attachment_ids[0] );
// ... then do whatever you need to do
} // No images found
else {
// #TODO
}
For more details: WP_Product class reference.
Related
I have a related posts slider but currently there is not enough posts to actually slide. The design is such that it's not a simple case of displaying the posts but not sliding them until there is more.
As a short term solution, I am trying to find a way to loop through the posts twice to give the appearance of an infinite loop.
I am using slick slider and initially tried the settings:
infinite: true,
loop: true
but I can't get it to work even though apparently that should do the trick.
I am now trying to just pull through the posts twice
I have tried adjusting the count to things like
$count+1;
$count = 2
$count+=5;
All sorts of variations but I think I am way off base.
I would appreciate any help or a point in the right documentation. I have been reading about iterations but I can't grasp how they would be included in this as I had help with this from another developer.
<div class="log-book-carousel">
<?php
$current_page_id = get_the_ID();
$args = [
'posts_per_page' => '6',
'post__not_in' => [$current_page_id]
];
$the_query = new WP_Query( $args ); ?>
<?php
// Start our WP Query
while ($the_query -> have_posts()) : $the_query -> the_post();
// Display the Post Title with Hyperlink
?>
<div class="slides match-height">
<a href="<?php the_permalink(); ?>" title="Read more of the blog post '<?php the_title_attribute(); ?>'">
<?php if (has_post_thumbnail()) : ?>
<div class="log-book-slider-image">
<?php the_post_thumbnail(); ?>
</div>
<?php endif; ?>
<div class="log-book-details-wrapper white-bg">
<h3 class="black log-title"><?php the_title(); ?></h3>
<div class="log-book-slider-excerpt ">
<p class="log-book-text"><?php sew_display_post_intro_block( $post->ID, 10 ); ?></p>
</div>
</div>
</a>
</div>
<?php
$count++;
endwhile;
wp_reset_postdata();
?>
</div>
I just started playing around in WordPress, and created a theme for fun.
Everything is working properly except when I click on an archives month or category, the page I'm taken to displays all of the blog posts just like the main page, even though the slug is correct.
I've created an archive page and category page, replicating the main page since I want the design to be the same.
<div class="recentBlogsWrapper">
<h3><?php single_cat_title(); ?><?php get_the_archive_title(); ?> Category</h3>
<div class="blogPostWrapper">
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 4,
);
$blogposts = new WP_Query($args);
while($blogposts->have_posts()) {
$blogposts->the_post();
?>
<a href="<?php echo the_permalink(); ?>" class="blogCard card">
<div class="blogHomeImgWrap">
<img class="blogPostImg" src="<?php echo get_the_post_thumbnail_url('get_the_ID'(),'full') ?>" />
</div>
<div class="blogPadding">
<h3><?php the_title(); ?></h3>
<p><?php the_time('F j, Y') ?></p>
</div>
</a>
<?php } wp_reset_query(); ?>
</div>
</div>
What am I missing with the query and how do I reference the link I just clicked on?
If you have placed your code inside archive.php you shouldn't need to use a WP_Query (custom query), but just use the bog standard WordPress loop like this:
<div class="recentBlogsWrapper">
<h3><?php single_cat_title(); ?><?php get_the_archive_title(); ?> Category</h3>
<div class="blogPostWrapper">
<?php
while ( have_posts() ) {
the_post();
?>
<a href="<?php echo the_permalink(); ?>" class="blogCard card">
<div class="blogHomeImgWrap">
<img class="blogPostImg" src="<?php echo get_the_post_thumbnail_url( get_the_ID(), 'full' ) ?>" />
</div>
<div class="blogPadding">
<h3><?php the_title(); ?></h3>
<p><?php the_time( 'F j, Y' ); ?></p>
</div>
</a>
<?php } ?>
</div>
</div>
Explanation: by using the following code as an argument in your custom loop
$args = array(
'post_type' => 'post',
'posts_per_page' => 4,
);
You were asking WordPress to get all (any) posts. But in reality, you want to only grab posts in a certain category when you're on the category archive. Luckily for you, WordPress does all this for you, so by removing your custom query you should be good to go.
I have created a 'Latest Products' loop by inserting the following code into my 'index.php' file:
<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>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</ul>
</section>
I now wish to be able to apply an arrow on the left and right of this row, where upon selecting the arrow, it will scroll through the images.
I am aware of the following codes:
<?php previous_post_link(); ?>
<?php next_post_link(); ?>
However, upon selection of their links, the user is taken to its dedicated page rather than being able to 'scroll' through the products.
Any suggestions on how I can achieve this?
Maybe you are looking for a carousel? There are a lot of jQuery plugins, i have used this one.
If you choose this solution, you should get the script, put it in your theme, add it, preferably in functions.php (more info here) and then add the carousel itself.
$('.row-fluid').owlCarousel({})
But insteal of all this maybe using a plugin like "WooCommerce Product Slider" or similar will be better. It provides shorcode and widget implementation which should work with most themes and can be used directly in the control panel withouth coding. One more note - if you still prefer to use your own code - maybe it'll be better to put it in a template file instead in index.php
I was make custom template for my WordPress site. So I need to show my product in my custom template dynamically. How can i do that?
This is my html code-->
<div class="all_content_shop">
<div class="col-md-3 single_all_c_s">
<div class="shop_product_inner">
<a href="<?php echo get_permalink( $loop->post->ID ) ?>">
<img src="http://jetsetbabies.com/wp-content/uploads/2006/07/100Juice_Organic_Apple_MAIN_v2.jpg" alt="Prduct Image" />
<div class="shop_product_inner_caption">
<h2>Product Title</h2>
<p>$200</p>
Add to Cart
</div>
</a>
</div>
</div>
</div>
================================================
Also i was make simple query but i did not found add to cart button dynamic code???
This is my query code---->
<div class="all_content_shop">
<?php $new_posts = new WP_Query(array(
'post_type' => 'product', //post of page of my post type
'cat' => 0, // category id, 0 for all categories.
'posts_per_page' => 12,
'offset' => 0, //how many posts you want to eliminate from the query
'orderby' => '', // order by title or date ?
'order' => 'DESC') // order as ASC or DESC
); ?>
<?php if ($new_posts->have_posts()) :
while ($new_posts->have_posts()) :
$new_posts->the_post(); ?>
<div class="col-md-3 single_all_c_s">
<div class="shop_product_inner">
<a href="<?php echo get_permalink( $loop->post->ID ) ?>">
<?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_single'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" />'; ?>
<div class="shop_product_inner_caption">
<h2><?php the_title(); ?></h2>
<p><?php if ( ! defined( 'ABSPATH' ) ) exit; global $post, $product; ?> <?php echo $product->get_price_html(); ?></p>
Add to Cart
</div>
</a>
</div>
</div>
<?php endwhile;//Possibility to add else statement ?>
<?php wp_reset_postdata(); ?>
<?php else:?>
<p class="not_found">Sorry, The post you are looking is unavailable!</p>
<?php endif; wp_reset_query(); ?>
</div>
So my question is--->
My query is right or wrong?? if my query is wrong, please give me right query... Also if my query is almost right so please give me just "Add to cart" (http://prntscr.com/439lxj) button dynamic Ajax code...
Please help me...
Please help me...
Thanks !!!
I have made the necessary changes for the code to work. Make sure woocommerce add-to-cart.js or add-to-cart.min.js is enqueued in the page.
<div class="all_content_shop">
<?php wc_print_notices(); ?>
<?php $new_posts = new WP_Query( array(
'post_type' => 'product', //post of page of my post type
'cat' => 0, // category id, 0 for all categories.
'posts_per_page' => 12,
'offset' => 0, //how many posts you want to eliminate from the query
'orderby' => '', // order by title or date ?
'order' => 'DESC'
) // order as ASC or DESC
); ?>
<?php if ( $new_posts->have_posts() ) :
while ( $new_posts->have_posts() ) :
$new_posts->the_post();
global $product;
$product = get_product( get_the_ID() ); //set the global product object?>
<div class="col-md-3 single_all_c_s">
<div class="shop_product_inner">
<a href="<?php the_permalink() ?>">
<?php if ( has_post_thumbnail( get_the_ID() ) ) {
echo get_the_post_thumbnail( get_the_ID(), 'shop_single' );
} else {
echo '<img src="' . woocommerce_placeholder_img_src() . '" alt="Placeholder" />';
} ?>
<div class="shop_product_inner_caption">
<h2><?php the_title(); ?></h2>
<p><?php echo $product->get_price_html(); ?></p>
<?php woocommerce_template_loop_add_to_cart(); //ouptput the woocommerce loop add to cart button ?>
</div>
</a>
</div>
</div>
<?php endwhile;//Possibility to add else statement ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p class="not_found">Sorry, The post you are looking is unavailable!</p>
<?php endif;
wp_reset_query(); ?>
I have the current loop working to display posts, but I can't seem to get the title or content to populate. What am I missing?
<?php
// LOOP ARGUMENTS
$args = array( 'post_type' => 'cbd_slider', 'posts_per_page' => -1 ); // -1 Shows ALL Posts
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
// CUSTOM CONTENT
$slideLink = get_post_meta($post->ID,"slideLink",true);
$thumb = wp_get_attachment_image_src(get_post_thumbnail_id(), 'thumbnail_name');
$imgURL = (isset($thumb[0]) ? $thumb[0] : get_template_directory_uri() . "/images/placeholder.jpg");
?>
<li class="clearfix"><!-- Start of featured slide -->
<a href="<?php echo $slideLink ?>">
<img src='<?php echo get_template_directory_uri(); ?>/thumb.php?src=<?php echo urlencode($imgURL); ?>&h=330&w=496' alt='featuredimage' /></a>
<div class="description">
<h2>TITLE GOES HERE</h2>
<p>POST CONTENT GOES HERE</p>
more
</div>
</li><!-- End of featured slide --><?php /* END WHILE AND RESET QUERY */ endwhile; wp_reset_query(); ?>
Have you tried the_title? Substituting <?php the_title(); ?> for your TITLE GOES HERE and <?php the_content(); ?> for CONENT GOES HERE?