I have given the a url path for image in wordpress using maya theme of woocommerce and the image is not showing the image path looks like
<img title="Adidas-H111-2" alt="Adidas-H111-2" class="attachment-shop_thumbnail" src="http://localhost/alphatest/wp-content/uploads/http://localhost/webservice/ap/upload_images/Adidas/Hard/H111/Adidas-H111-2.jpg" style="opacity: 1;">
how can I remove the part
http://localhost/alphatest/wp-content/uploads/
I am using maya theme and woocommerce plugin in wordpress. the full code is like
<?php
/**
* Single Product Thumbnails
*/
global $post, $woocommerce;
?>
<div class="thumbnails">
<?php
$thumb_id = get_post_thumbnail_id();
$small_thumbnail_size = apply_filters('single_product_small_thumbnail_size', 'shop_thumbnail');
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->ID,
'post__not_in' => array($thumb_id),
'post_mime_type'=> 'image',
'orderby' => 'menu_order',
'order' => 'ASC'
);
$attachments = get_posts($args);
if ($attachments) :
$loop = 0;
$columns = apply_filters('woocommerce_product_thumbnails_columns', 3);
foreach ( $attachments as $attachment ) :
if (get_post_meta($attachment->ID, '_woocommerce_exclude_image', true)==1) continue;
$loop++;
$_post = & get_post( $attachment->ID );
$url = wp_get_attachment_url($_post->ID);
$post_title = esc_attr($_post->post_title);
$image = wp_get_attachment_image($attachment->ID, $small_thumbnail_size);
echo '<a href="'.$url.'" title="'.$post_title.'" rel="thumbnails" class="zoom ';
if ($loop==1 || ($loop-1)%$columns==0) echo 'first';
if ($loop%$columns==0) echo 'last';
echo '">'.$image.'</a>';
endforeach;
endif;
?>
</div>
Simply add Double Slash (//)Instead of the Domain name. Example:
http://www.google.com/wp-content/uploads/2013/10/image.png
will be as:
//wp-content/uploads/2013/10/image.png
Related
I face a problem with displaying products on a specific portfolio post. I use product shortcodes in order to display specific products, but they do not appear.
I use wordpress 5.2.5, avada theme 5.9.1 and woocommerce 3.9.2.
Anyone got an idea what is going on?
If not, can anyone tell me an alternative way to display them?
use shortcode [get_specific_product_shortcode id='here enter product id']
function get_specific_product_shortcode_function($atts){
extract( shortcode_atts(
array(
'id' => '',
), $atts )
);
ob_start();
$args = array(
'posts_per_page' => 1,
'post__in' =>array($id),
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'product',
'post_status' => 'publish',
);
$query = new WP_Query( $args );
$count = $query->found_posts;
if($query->have_posts()){
while( $query->have_posts() ) { $query->the_post();
$post_thumbnail_id = get_post_thumbnail_id();
$product = wc_get_product(get_the_ID());
if (!empty($post_thumbnail_id)) {
$image_size = 'medium'; // (thumbnail, medium, large, full or custom size)
$img_url_arr = wp_get_attachment_image_src( $post_thumbnail_id, $image_size );
$img_url = $img_url_arr[0];
}else{
//placeholder image
$img_url = home_url().'/wp-content/uploads/woocommerce-placeholder.png';
}
?>
<div>
<a href="<?php echo get_permalink();?>">
<img src="<?php echo $img_url?>" style="width: 150px;">
</a>
<div>Price: <?php echo $product->get_price_html(); ?></div>
<div><?php echo get_the_title();?></div>
</div>
<?php
}
wp_reset_postdata();
}else{
echo 'no post'; echo "<br>";
}
return ob_get_clean();
}
add_shortcode('get_specific_product_shortcode', 'get_specific_product_shortcode_function');
Maybe this questions already asked but that not helpful for my question.
Am creating a API for my WordPress project. So i want to send API for to get all products with pagination.
I get products list using this code:
android/all_products.php
<?php
require_once('../wp-load.php');
$args = array(
'post_type' => 'product',
'posts_per_page' => 10
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
echo '<br />' . woocommerce_get_product_thumbnail().' '.get_the_title().'';
endwhile;
wp_reset_query();
?>
I got all products, but i want to show products with pagination.
Note: all the API's are written in inside android folder.
Thanks in advance.
below pagination code is to show next products you can also do it to show previous product by applying same method
if(isset( $_GET['page_num'] ) ) {
$page_number = $_GET['page_num'];
} else {
$page_number = 2;
}
$args = array (
'limit' => 3,
'page' => $page_number,
);
$products = wc_get_products( $args );
foreach( $products as $product ) {`enter code here`
//your data here
// e.g $product_name = $product->get_name();
}
Next
If you want to create API's then just use Woocommerce rest API's which are built in function given in wordpress please use below url it will give you list of products with lots of options:
{{your_url}}/wp-json/wc/v2/products
Go to wp-admin and go to Woocommerce -> settings -> API
-> Enable rest API
-> Go to Key/Apps and create Auth Keys both secret and Private (Copy that keys)
Then give this above url to add it for use in application with o_auth of secret key and private key.
Please see this link for all woocommerce API's : https://woocommerce.github.io/woocommerce-rest-api-docs/#retrieve-a-product
Finally i creating a code and send the date using API.
<?php
require_once('../wp-load.php');
$args = array(
'post_type' => 'product',
'posts_per_page' => -1
);
$brand_product_list = new WP_Query( $args);
$pagination_count = 1;
while($brand_product_list->have_posts()) : $brand_product_list->the_post();
$pagination_count++;
endwhile; wp_reset_query();
//echo'<pre>';print_r($pagination_count/12); exit;
wp_reset_query();
?>
<?php
$pagination = round($pagination_count/12);
for($i=1;$i<=$pagination;$i++)
{
$pagination_no[] = $i;
?>
<!-- <a class="product-category-view-all" href="?pagination=<?php echo $i; ?>"><?php echo $i; ?></a> -->
<?php } ?>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : $_GET['pagination'];
$args = array(
'post_type' => 'product',
'paged' => $paged,
'posts_per_page' => 12,
);
$wp_query = new WP_Query($args);
while($wp_query->have_posts()) : $wp_query->the_post();
$product_data = wc_get_product( $post->ID );
if(!empty(get_the_post_thumbnail_url($product_data->get_id())))
{
$img = get_the_post_thumbnail_url($product_data->get_id());
}
else
{
$img = "";
}
$product_list[] = array(
'product_id' => $product_data->get_id(),
'product_name' => $product_data->get_title(),
'product_regular_price' => $product_data->get_regular_price(),
'product_sale_price' => $product_data->get_sale_price(),
'product_price' => $product_data->get_price(),
'img' => $img,
'rating' => $product_data->get_average_rating(),
'stock_quantity' => $product_data->get_stock_quantity(),
'stock' => $product_data->is_in_stock(),
);
endwhile; wp_reset_query();
$data[] = array(
'pagination' => $pagination_no,
'product_list' => $product_list
);
//echo json_encode($peoduct_list, $pagination)
echo json_encode($data)
?>
In the wordpress front-page.php theme file i loop through all the woocoommerce products and show the featured-image.
<ul class="products">
<?php
// Setup your custom query
$args = array( 'post_type' => 'product' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li>
<a href="<?php echo get_permalink( $loop->post->ID ) ?>">
<?php the_post_thumbnail( ); ?>
</a>
</li>
<?php endwhile; wp_reset_query(); // Remember to reset ?>
</ul>
Now i have some products which are variable products (different colors). Every color variation has its own image. How can i show all the different variation-images inside this loop? My plan is to create an image-slider with these images.
<?php
$args = array( 'post_type' => 'product' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$product_s = wc_get_product( $loop->post->ID );
if ($product_s->product_type == 'variable') {
$args = array(
'post_parent' => $plan->ID,
'post_type' => 'product_variation',
'numberposts' => -1,
);
$variations = $product_s->get_available_variations();
echo '<pre>';
print_r($variations);
// You may get all images from $variations variable using loop
echo '</pre>';
}
endwhile; wp_reset_query(); // Remember to reset ?>
I do not have tested yet. But hopefully it will work.
Use below code to get image urls
For Wc3+
foreach ( $variations as $variation ) {
echo $variation['image']['url'];
}
And for old Wc versions
foreach ( $variations as $variation ) {
echo $variation['image_src'];
}
$args = array(
'post_type' => 'product',
'posts_per_page' => 10,
);
$product_arrray = get_posts($args);
foreach($product_arrray as $prod)
{
$product_id = $prod->ID;
$product = wc_get_product($product_id);
$pvariation = $product->get_available_variations();
echo "<pre>";print_r($pvariation);echo "</pre>";
}
I want show featured image in my slider. so what i've done in my function.php is like below:
<?php
function revconcept_get_images($post_id) {
global $post;
$thumbnail_ID = get_post_thumbnail_id();
$images = get_children( array('post_parent' => $post_id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID') );
if ($images) :
foreach ($images as $attachment_id => $image) :
$img_alt = get_post_meta($attachment_id, '_wp_attachment_image_alt', true); //alt
if ($img_alt == '') : $img_alt = $image->post_title; endif;
$big_array = image_downsize( $image->ID, 'large' );
$img_url = $big_array[0];
echo '<li>';
echo '<img src="';
echo $img_url;
echo '" alt="';
echo $img_alt;
echo '" />';
echo '</li><!--end slide-->';
endforeach; endif; }
?>
<div class="flexslider"> <!-- function called in index.php -->
<ul class="slides">
<?php revconcept_get_images("$post->ID"); ?>
</ul>
</div><!--end flexslider-->
Now i have two problem. no.1 My featured image is not showing in the slider. It's showing a broken link. but it can echo the post_id. no.2 I want show only 3/4 images from the latest post of cat=5. can anyone help me to do this ? note that i am using flexslider for my slider.
Ok, this is what I've tested, you may want to refine the query a bit, but for me it worked. In functions.php put
function revconcept_get_images($post_id) {
$images = get_posts( array(
'post_type' => 'attachment',
'hide_empty' => true,
'order' => 'ASC',
'orderby' => 'menu_order ID'
));
foreach ($images as $image) {
$image_url = $image->guid;
$img_alt = get_post_meta($image->ID, '_wp_attachment_image_alt', true); //alt
echo '<li><img src="'.$image_url.'" alt="'.$img_alt.'" /></li>';
}
}
And in index.php put (where you want)
<div class="flexslider"> <!-- function called in index.php -->
<ul class="slides">
<?php revconcept_get_images($post->ID); ?>
</ul>
</div><!--end flexslider-->
If you want to show all featured images from posts you have just put 'posts_per_page' => -1, in the get_posts() array.
I'm developing a website with Woocommerce plugin installed. I want my shop page to show the category name first & then display the products within that category, then next category name with the products it has.
already searched in google & here, found a solution, but it is not what I want. it added the category name after the price area of every product. I'm not a pro, so having a difficult time to solve this problem.
function wc_category_title_archive_products(){
$product_cats = wp_get_post_terms( get_the_ID(), 'product_cat' );
if ( $product_cats && ! is_wp_error ( $product_cats ) ){
$single_cat = array_shift( $product_cats ); ?>
<small class="product_category_title"><?php echo $single_cat->name; ?></small>
<?php }
}
add_action( 'woocommerce_after_shop_loop_item', 'wc_category_title_archive_products', 5 );
<?php
$cat_args = array(
'parent' => '0',
'taxonomy' => 'product_cat'
);
$categories = get_categories( $cat_args );
foreach ($categories as $category) {
echo $category->cat_name;
echo do_shortcode('[product_category category="'.$category->cat_name.'" per_page="12" columns="4" orderby="date" order="DESC"]');
}
?>
SHOW PRODUCT CATEGORY NAME ABOVE TITLE IN SHOP PAGE ==> Check on Woocomerce Version 3.5.3 You need to just copy it and paste it to your theme functions.php
function wpa89819_wc_single_product(){
$product_cats = wp_get_post_terms( get_the_ID(), 'product_cat' );
if ( $product_cats && ! is_wp_error ( $product_cats ) ){
$single_cat = array_shift( $product_cats ); ?>
<h2 itemprop="name" class="product_category_title"><span><?php echo $single_cat->name; ?></span></h2>
<?php }
}
add_action( 'woocommerce_after_shop_loop_item_title', 'wpa89819_wc_single_product', 5 );
You're going to have to do a new loop/query for each category. We can automate some of the process by using WordPress's get_categories function. Here's an example::
<?php
$cat_args = array(
'parent' => '0',
'taxonomy' => 'product_cat'
);
$categories = get_categories( $cat_args );
foreach ($categories as $category) { ?>
<ul class="product-category">
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 1, 'cat' => $category->cat_ID, 'orderby' => 'rand' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<!-- Your output -->
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</ul>
}
?>