I want to fetch all woocommerce categories with title and images. and after that, I wanted to display them on a front-page. Avoiding foreach loop becuase my front-page design is something like this one.
[enter image description here][1]
<?php
$termSlug = array();
$tax_terms = get_terms('product_cat', array('hide_empty' => '0'));
foreach ( $tax_terms as $tax_term ):
$termSlug[] = $tax_term->slug;
endforeach;
$args = array(
'post_type' => 'product', //Post type event
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $termSlug
)
)
);
// The Query
$the_query = new WP_Query( $args );
//echo '<pre>';print_r($the_query);echo "</pre>";
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
?>
<?php
}
}else{
?>
<h2 style='font-weight:bold;color:#fff'>Nothing Found</h2>
</div>
<?php } echo '</div>';?>
[1]: https://i.stack.imgur.com/A98em.jpg
Following snippet will fetch product categories in WooCommerce. After fetching categories, it will display category name with link and category image in loop.
$product_categories = get_terms( 'product_cat', 'hide_empty=0' );
if ( ! empty( $product_categories ) && ! is_wp_error( $product_categories ) ) {
foreach ( $product_categories as $category ) {
?>
<div class="category-item">
<h4><?php echo esc_html( $category->name ); ?></h4>
<?php
$thumbnail_id = get_term_meta( $category->term_id, 'thumbnail_id', true );
$image = wp_get_attachment_url( $thumbnail_id );
?>
<?php if ( $image ) : ?>
<img src="<?php echo esc_url( $image ); ?>" alt="" />
<?php endif; ?>
</div>
<?php
}
}
Related
Hello I am using advanced custom field plugin for displaying some html only for selected products or only for products for selected categories in option page with relationship picker. My result instead is displaying for all products. This is what I have:
$products = get_field('products_picker', 'option');
$categories = get_field('categories_picker', 'option');
$prom_img = get_field('prom_img', 'option');
if ( $products ) {
foreach( $products as $p ):
if( $post->ID == $p->ID ):
<img src="<?php echo $prom_img['url']; ?>">
endif;
endforeach;
}
if ( $categories ) {
foreach( $categories as $term ):
$category = get_term( $term );
if( has_term( $category, 'product_cat', $post )) {
?>
<img src="<?php echo $prom_img['url']; ?>">
endif;
endforeach;
}
In similar cases I create a new wp_query passing the array with the post IDs like below:
$posts = get_field('products_picker', 'option');
$new_query = new WP_Query(array(
'post_type' => array('post'),
'post__in' => $posts,
'orderby' => 'post__in',
));
if ( $new_query->have_posts() ) :
while ( $new_query->have_posts() ) : $new_query->the_post();
//your code here
endwhile;
endif;
For the categories part you can use a query like this:
$categories = get_field('categories_picker', 'option');
$args = array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => $categories[0]
)
)
);
$cat_query = new WP_Query($args);
I've not tested this in Wordpress, but the first thing I would try is to rename the $post variable that pulls the ACF field to '$posts' (plural).
Then change the foreach loop as follows.
$posts = get_field('products_picker', 'option');
$prom_img = get_field('prom_img', 'option');
if ( $posts ) {
foreach( $posts as $post ):
setup_postdata($post);
$sales_html = '<div class="promo-badge test"><img src=' . $prom_img['url'] . '></div>';
wp_reset_postdata();
endforeach;
}
That might fix it... I'm not sure you won't need to turn the foreach loop into an actual Wordpress style loop (the type with while(have_posts()): the_post(); etc. like in #kaize answer below)
See here if my solution didn't help: https://www.advancedcustomfields.com/resources/querying-relationship-fields/
I have created a custom post type named "State Leagues" using types plugin. My post type has some categories and outputs both. When I display the posts' titles it displays correctly. My code for displaying post titles is this:
<?php
$args = array(
'post_type' => 'stateleague-pos-type',
'posts_per_page' => 4,
'order_by' => 'post_date',
'order' => 'DESC'
);
$loop = new WP_Query($args);
// $counter = 0;
// $big = 1;
while ($loop->have_posts()) : $loop->the_post();
?>
<a href="<?php the_permalink(); ?>">
<h1> <?php the_title(); ?> </h1>
</a>
?>
Now I want to display the categories' names as the title instead of the posts' titles. I have searched for it but didn't find anything that works. Is it possible to display category names as titles?
<?php
$taxonomy = 'filters';
$terms = get_terms($taxonomy);
if ( $terms && !is_wp_error( $terms ) ) :
?>
<ul>
<?php foreach ( $terms as $term ) { ?>
<li><?php echo $term->name; ?></li>
<?php } ?>
</ul>
<?php endif;?>
Or in fuctions.php Put this:
function get_the_category_custompost( $id = false, $tcat = 'category' ) {
$categories = get_the_terms( $id, $tcat );
if ( ! $categories )
$categories = array();
$categories = array_values( $categories );
foreach ( array_keys( $categories ) as $key ) {
_make_cat_compat( $categories[$key] );
}
return apply_filters( 'get_the_categories', $categories );
}
and call the function as:
<?php $cat = get_the_category_custompost($post->ID, 'Your Custom Taxonomy'); ?>
Try this i hope it will help
I was looking around google and here but nothing found about this.
<div class="thumbBlock">
<?php foreach (get_categories() as $cat) : ?>
<div class="thumbInside">
<a href="<?php echo get_category_link($cat->term_id); ?>" title="<?php echo $cat->name; ?>" />
<img src="POST_THUMBNAIL HERE FOR EACH CATEGORY" alt="<?php echo $cat->name; ?>" />
</a>
<p><?php echo $cat->cat_name; ?></p>
</div>
<?php endforeach; ?>
</div>
as u see I want to list categories in a custom template but in the place post thumbnail I want to grab last post thumb for each category is it possible?
I think you'll find the_terms and get_the_terms to be better functions
$terms = get_terms( 'my_taxonomy' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
echo '<ul>';
foreach ( $terms as $term ) {
echo '<li>' . $term->name . '</li>';
/* use the term id to find matching posts in this taxonomy */
$args = array (
'showposts' => 1,
'orderby' => 'date',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'my_taxonomy',
'field' => 'term_id',
'terms' => $term->term_id,
),
),
);
$new_query = new WP_Query( $args );
if ( $new_query->have_posts() ) {
while ( $new_query->have_posts() ) {
$new_query->the_post();
$post_id = get_the_ID();
echo '<li>';
echo get_the_post_thumbnail( $post_id, 'thumbnail' );
echo '</li>';
} // end while
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
}
echo '</ul>';
}
This hasn't been tested, but should get ya pretty close.
I want to show category thumbnails in loop which has n price of products.
Here n price will put from admin panel where a amount will fill and that products-category thumbnail will appear on a page which having that amount or greater than that amount of products.
so please help me to write a query or any other solution if you can give me.
I tried this code and get succeed.
//backend option for price
$value= get_option('price');
// the query
$args = array ('meta_query' => array(
array( 'key' => '_price',
'value' => $value,
'compare' => '>=',
'type' => 'NUMERIC',
),
),
'post_type' => 'product',
);
// The Query
$query = new WP_Query( $args ); ?>
<?php if ( $query->have_posts() ) : ?>
<!-- the loop -->
<?php $stack = array();?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<?php $a = $product->get_categories(); ?>
<?php if (!in_array($a, $stack)) : ?>
<li class="product-category">
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ){
$category_name = $term->name;
$category_id = $term->term_id;
$category_slug = $term->slug;
$category_thumbnail = get_woocommerce_term_meta($term->term_id, 'thumbnail_id', true);
$image = wp_get_attachment_url($category_thumbnail);
echo '<img class="absolute category-image" src="'.$image.'">';
}
echo '' . $category_name . '';
?>
I'm trying to get the attachments of a specific term (in its archive page).
But the results are showing the resulting images 5 times instead of one.
I have multiple loops in this page - one to show related posts, another to show related products (custom post), and this one to show related images. Custom posts and posts are working nicely, but I can't show the attachments in the right way. :S
<?php $queried_object = get_queried_object();
$term_id = $queried_object->term_id;
$args = array(
'post_status' => 'inherit',
'numberposts' => 0,
'post__not_in' => array_merge($do_not_duplicate,get_option( 'sticky_posts' )),
'post_type' => 'attachment',
);
$args['tax_query'] = array(
array(
'taxonomy' => 't-arte',
'terms' => $term_id,
'field' => 'id',
),
); ?>
<?php $t = $data['t-arte'];
$array = explode(" ", $t);
$array = array_unique($array);?>
<?php $media_query = array_unique($array); ?>
<?php $media_query = get_posts($args);
if( !empty( $media_query ) ) :
foreach ($media_query as $media_query) :
global $post; $post = $media_query;
setup_postdata($media_query);
?>
<div id="archivespage-media-item">
<?php $attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
echo '<div id="imagem">';
the_attachment_link( $attachment->ID, true );
echo '</div>';
}
}?>
</div>
<?php endforeach;else :?>
<p>Ainda não temos nenhuma imagem relacionada :(</p>
</div>
<?php endif; ?>
<?php wp_reset_query();?>'
I've done these and its working!
The result will show all attachments in a specific term inside term's archive page.
<?php $queried_object = get_queried_object();
$term_id = $queried_object->term_id;
global $wp_query;
$original_query['tax_query'] = array(
array(
'taxonomy' => 't-arte',
'terms' => $term_id,
'field' => 'id',
),);
$original_query = (array) $wp_query;
$attach_query = array(
'post_type'=> array( 'attachment' ),
'post_status' => array( null ));
$args = array_merge($original_query['query_vars'], $attach_query);
$media_query = new WP_Query( $args )?>
<?php if($media_query->have_posts()) :
while ($media_query->have_posts() ) : $media_query->the_post();
if( $post->ID == $do_not_duplicate ) continue; ?>
<div id="archivespage-media-item">
<div id="imagem">
<?php echo wp_get_attachment_link($attachment->ID, 'bigger-thumb');?>
</div>
</div>
<?php endwhile; else: ?>
//do stuff
</div>
https://wordpress.stackexchange.com/questions/29635/how-to-create-an-attachments-archive-with-working-pagination