get_queried_object()->image ; cannot get image - php

I have created a custom taxonomy for products. In custom taxonomy user can enter name and can upload a image. In the products page I can get the image and title of category. When user clicks on this it moves to next page where description of post and title is shown.
I also want to get image also in this next page.
Here is the code: For showing custom categories
foreach($terms as $term) {
$prod_meta = get_option("taxonomy_term_".$term->term_id);
if(($counter % 4) == 0) {
echo '<div class="row product-gallery">';
}
?>
<div class="col-sm-3">
<div class="product-warp">
<div class="product">
<img src="<?php echo $prod_meta['img']; ?>" title="" alt="">
</div>
<div class="product-name">
<h5>
<a href="<?php echo get_term_link($term->slug, 'product_categories') ?>">
<?php echo $term->name; ?>
</a>
</h5>
</div>
</div>
</div>
For product detail page
<?php
get_header();
$slug = get_queried_object()->slug; // get clicked category slug
$name = get_queried_object()->name; // get clicked category name
$tax_post_args = array(
'post_type' => 'products', // your post type
'posts_per_page' => 999,
'orderby' => 'id',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'product_categories', // your taxonomy
'field' => 'slug',
'terms' => $slug
)
)
);
$tax_post_qry = new WP_Query($tax_post_args);
if ($tax_post_qry->have_posts())
while($tax_post_qry->have_posts()) :
$tax_post_qry->the_post(); ?>
<div class="row product-details">
<div class="col-sm-7">
<div class="fire-product">
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>

Related

Woocommerce how to display products which has two categories, one of them should be common

I am developing custom Woo shop theme. I want to display in front-page.php NEW COLLECTION products, it should be one block, and inside few smaller blocks with products ant their another vcategories, because, they will have common category NEW and another category depends BEDROOM/BATHROOM/CLOTHING, for now i have reahced display by one category.
<div class="col-md-12 row">
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 4,
'product_cat' => 'clothing'
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
$colors = explode(',',$product->get_attribute('color'));
$price = $product->get_price_html();
?>
<div class="col-md-3 d-flex flex-column">
<div class="homepage__single-product">
<a href="<?php the_permalink(); ?>">
<img src="<?php echo the_post_thumbnail_url(); ?>">
<p class="homapage__single-product-title"><?php the_title(); ?></p>
<p class="homapage__single-product-price"><?php echo $price ?></p>
<p class="homepage__single-product-color">
<?php foreach ($colors as $color) {
echo '<span class="attribute-color '. strtolower(trim($color)) .'"></span>';
}
?>
</p>
</a>
</div>
</div>
<?php
endwhile;
wp_reset_query();
?>
</div>
I hope that my question will be clear, i am adding image with design for better understanding. I think that it should be somethimg like this code, but this code should have NEW as product_cat in $args, and then inside loop somehow do same block but with other categories.
design shows how it should be
You can use tax_query to get products from multiple categories. check below code.
<div class="col-md-12 row">
<?php
$prod_categories = array( 'bedroom', 'bathrooms', 'clothing'); //category slugs
$args = array(
'post_type' => 'product',
'posts_per_page' => 4,
);
if (!empty($prod_categories)) {
$args['tax_query'] = array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $prod_categories,
'operator' => 'IN',
));
}
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
$colors = explode(',',$product->get_attribute('color'));
$price = $product->get_price_html();
?>
<div class="col-md-3 d-flex flex-column">
<div class="homepage__single-product">
<a href="<?php the_permalink(); ?>">
<img src="<?php echo the_post_thumbnail_url(); ?>">
<p class="homapage__single-product-title"><?php the_title(); ?></p>
<p class="homapage__single-product-price"><?php echo $price ?></p>
<p class="homepage__single-product-color">
<?php foreach ($colors as $color) {
echo '<span class="attribute-color '. strtolower(trim($color)) .'"></span>';
}
?>
</p>
</a>
</div>
</div>
<?php
endwhile;
wp_reset_query();
?>
</div>

Wordpress - Category image

I know this question was asked so many times, but none of the solutions are working for me.
I want to display my categories on the website with their thumbnails together.
I tried to use category imagies plugin, but for some reason this plugin stopped working and doesn't give me the opportunity to attach picture to the category (the add image button isn't shown in the category settings).
After this I created custom field, where I made a taxonomy is equal to category. With this I could pair images with categories, but I don't know how to add it to the code.
Here is my code:
<section id="categories">
<div class="center-box">
<div class="categories">
<?php
$categories = get_categories(array ('parent' => 0));
foreach($categories as $category) { ?>
<div class="category-box">
<a class="category-img" href="<?php echo get_category_link($category->term_id); ?>">
<img class="img" src="<?php echo z_taxonomy_image_url($category->term_id); ?>"/>
<div class="category-name">
<h3> <?php echo $category->name; ?> </h3>
</div>
</a>
</div>
<?php
$args = array(
'numberposts' => 1,
'orderby' => 'date',
'order' => 'DESC',
'category' => $category->term_id, );
?>
<?php } ?>
</div>
</div>
</section>
It still has the previous category imagies plugin code which doesn't work anymore.
Someone could help me how to display the categories with image?
Thank you!
I'd suggest using the Advanced Custom Fields plugin for this, if you are not already. Under the working assumption that you are, you could do the following:
<section id="categories">
<div class="center-box">
<div class="categories">
<?php
$categories = get_categories(array ('parent' => 0));
foreach($categories as $category) { ?>
<div class="category-box">
<a class="category-img" href="<?php echo get_category_link($category->term_id); ?>">
<img class="img" src="<?php echo wp_get_attachment_image_src( get_field( 'thumbnail', "term_$category->term_id" ), 'small' ) ?>"/>
<div class="category-name">
<h3> <?php echo $category->name; ?> </h3>
</div>
</a>
</div>
<?php
$args = array(
'numberposts' => 1,
'orderby' => 'date',
'order' => 'DESC',
'category' => $category->term_id, );
?>
<?php } ?>
</div>
</div>

How can I retrieve image and title in php from custom post type plugin of wordpress

How can i get dynamic data of image and title from this following code? I am currently working on this code but unable to retrieve data. I am attaching screenshot of slider where nothing is displaying as well as i am also adding screenshot of demo slider.
my front-page.php:
<?php $our_services = get_our_services_list(5); ?>
<div class="col-sm-12">
<div class="our-services-slider">
<?php
if(!empty($our_services) && count($our_services) > 0){
for($i=0; $i<count($our_services); $i++) {
?>
<div class="slider-tile">
<div class="tile-img">
<img src="<?php echo $services_image; ?>" alt="">
</div>
<div class="tile-text">
<h4>
<b><?php echo $post_title; ?></b>
</h4>
<p><?php echo $post_excerpt; ?></p>
<div class="text-center">
<a class="btn btn-mehroon" href="<?php echo $services_button_link; ?>">
<?php echo $services_button; ?></a>
</div>
</div>
</div>
<?php } } ?>
my functions.php:
function get_our_services_list($per_page_record = -1){
$args = array(
'posts_per_page' => $per_page_record,
'orderby' => 'ID',
'order' => 'DESC',
'post_type' => 'our_services'
);
return get_posts($args);
}
Demo page
my page:
you should try this :
<?php
$args = array(
'post_type' => 'client_testimonial',
'post_status' => 'publish',
'posts_per_page'=> 6,
'order' => 'DESC',
'orderby' => 'date'
);
$query = new WP_Query($args);
?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
// you can get image like this :
<?php $img = get_the_post_thumbnail_url(get_the_ID()); ?>
// get title like this:
echo get_the_title(get_the_ID());

Getting multiple image of same Title

Taxonomy image
page image
Here, I have categorized my page with different page. In that, I categorized with taxonomy and slug. All the title have different taxonomy as shown in the image. But here I am getting multiple image because it have multiple taxonomy.
So, I just want 8 images as the Business theme are as shown in images and have to display as per the taxonomy.
As you can see the taxonomy count that is 4,4 and 5, So I am getting record like that, but I want to get record as per Business theme, as they are eight but as per taxonomy.
Here what I have tried:
<div class="portfolio-items">
<?php
$terms = get_terms('portfolio_cat');
foreach($terms as $row)
{
$args = array('post_type' => 'portfolio','tax_query' => array(
array(
'taxonomy' => 'portfolio_cat',
'field' => 'slug',
'terms' => array( $row->slug ),
)
));
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post();
$i=1;
?>
<div class="portfolio-item <?php echo $row->slug; ?> col-xs-12 col-sm-4 col-md-3">
<div class="recent-work-wrap">
<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'thumbnail' ); ?>
<?php $large_url = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'large' ); ?>
<img class="img-responsive" src="<?php echo $url; ?>" alt="">
<div class="overlay">
<div class="recent-work-inner">
<h3><?php the_title(); ?></h3>
<?php $content = get_the_content(); ?>
<p><?php echo $content; ?></p>
<a class="preview" href="<?php echo $large_url; ?>" rel="prettyPhoto"><i class="fa fa-eye"></i> View</a>
</div>
</div>
</div>
</div><!--/.portfolio-item-->
<?php
// End of the loop.
$i++;
endwhile;
endif;
WP_Reset_Query();
}
?>
</div>

single-product.php not opening

I have created a set of pages. In first page product.php all the custom categories are shown (title and image). If a user clicks on it then it moves to the taxonomy-product_categories page where products are shown of the specific category. Now i want that if user clicks on the product then it goes to single-product.php page.
code is here
<?php
get_header();
$slug = get_queried_object()->slug; // get clicked category slug
$name = get_queried_object()->name; // get clicked category name
$tax_post_args = array(
'post_type' => 'products', // your post type
'posts_per_page' => 999,
'orderby' => 'id',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'product_categories', // your taxonomy
'field' => 'slug',
'terms' => $slug
)
)
);
?>
<div id="main-wrap">
<div class="container">
<?php
$counter = 1;
$tax_post_qry = new WP_Query($tax_post_args);
if ($tax_post_qry->have_posts()) {
while ($tax_post_qry->have_posts()) {
$tax_post_qry->the_post();
$the_url = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), $type);
//var_dump($the_url);
if ($counter % 4 == 0) {
echo '<div class="row product-gallery">';
}
?>
<div class="col-sm-3">
<div class="product-warp">
<div class="product">
<img src="<?php echo $the_url[0] ?>" title="" alt="">
</div>
<div class="product-name">
<h5>
<a href="">
<?php echo get_the_title();
; ?>
</a>
</h5>
</div>
</div>
</div>
<?php
if ($counter % 4 == 0) {
echo '</div>';
}
?>
<?php
}
}
?>
</div>
</div>
<?php get_footer(); ?>
this is the place where user clicks then it should go to single-product.php
<a href="#"><img src="<?php echo $the_url[0] ?>" title=""
Somebody guide me step by step please
If your post_type => products Then your file name must be like this single-products.php
Read this article
And for precautions (if the page not open yet. means give 404 error) go to
admin panel
settings
permalinks
First save permalinks as plain and refresh your site home page.
Then save permalinks as post_name and again refresh your home page
Hope this will help you.
Please Try
'post_type' => 'products'
Your Post type is “products”.
And wordpress ruls is “single-{post_type}.php”.
You make “single-products.php” and remove “single-product.php”
https://codex.wordpress.org/Post_Type_Templates

Categories