I have made this code and the variable $link is not returning the product url, instead is returning home url. Please some Help! What I want is to get the product link selecting it by category. The display is like a menu by category but with the link going directly to one product I selected by category.
<ul>
<?php
$categories= get_terms( 'product_cat', array( 'parent' => 15, 'hide_empty' => false ) );
foreach($categories as $category): ?>
<li>
<div class="grid-subheader">
<?php
$metaArray = get_option('taxonomy_' . $category->term_id);
$productCatMetaTitle = $metaArray['wh_meta_title'];
$type = get_term_by( 'id', $category->parent, 'product_cat' );
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => 1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id', //This is optional, as it defaults to 'term_id'
'terms' => $category->term_id,
'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
)
)
);
$products = new WP_Query($args);
foreach ($products as $prod){
$link = get_the_permalink($prod->ID);
}
?>
<h3><?php echo $productCatMetaTitle; ?></h3>
<p><?php echo $category->description; ?></p>
<a href="<?php echo $link; ?>">
<div class="button-menu">
<?php //echo $type->name; ?> <?php echo $category->name; ?>
</div>
</a>
</div>
</li>
<?php endforeach; ?>
</ul>
Try the following revisited code:
<ul>
<?php
$taxonomy = 'product_cat';
$category_terms = get_terms( $taxonomy, array( 'parent' => 15, 'hide_empty' => false ) );
foreach( $category_terms as $term ) { ?>
<li>
<div class="grid-subheader">
<?php
$meta = get_option('taxonomy_' . $term->term_id);
$title = isset($meta['wh_meta_title']) ? $meta['wh_meta_title'] : '';
$type = get_term_by( 'id', $term->parent, $taxonomy );
$query = get_posts( array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => 1,
'fields' => 'ids',
'tax_query' => array( array(
'taxonomy' => $taxonomy,
'field' => 'term_id',
'terms' => array( $term->term_id ),
) ),
) );
$product_id = reset($query);
?>
<h3><?php echo $title; ?></h3>
<?php if ( ! empty($term->description) ) ?>
<p><?php echo $term->description; ?></p>
<a href="<?php echo get_permalink($product_id); ?>">
<div class="button-menu"><?php echo $term->name; ?></div>
</a>
</div>
</li>
<?php
}
?>
</ul>
Related
I've a custom loop which should display 3 projects per category in a random order.
The problem is, that the loop always shows all projects in that category.
I've tested the the code on a single page and it works how it should and only showed 3 projects.
So I guess it has something to do with the archive page?!
Here's ist the loop itself:
<?php
$args_thema = array (
'post_type' => array( 'project' ),
'orderby' => 'rand',
'posts_per_page' => 3,
'paged' => $paged,
//'ignore_sticky_posts' => 1,
'tax_query' => array(
array(
'taxonomy' => 'project-category',
'field' => 'slug',
'terms' => $category->slug,
),
),
);
$query_thema = new WP_Query( $args_thema ); if ( $query_thema->have_posts() ) : ?>
<ul class="">
<?php $i = 0; while ( $query_thema->have_posts() ) : $query_thema->the_post(); $i++; // echo $i; ?>
<li class="">
<a class="url uid" href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php else : ?>
<?php endif; wp_reset_postdata(); ?>
And here's the full code (I guess not that relevant):
<?php
$args = array('hide_empty' => '0', 'taxonomy' => 'project-category', 'orderby' => 'name', 'order' => 'ASC', 'parent' => 0 );
$categories = get_categories($args);
foreach($categories as $category) {
?>
<div class="row">
<div class="col-lg-4">
<?php
$term_child = get_term_by('slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$args_child = array('hide_empty' => '0', 'taxonomy' => 'project-category', 'orderby' => 'name', 'order' => 'ASC', 'parent' => $category->term_id );
?>
<h4><?php echo $category->name; ?></h4>
</div>
<div class="col-lg-8">
<?php
$args_thema = array (
'post_type' => array( 'project' ),
'orderby' => 'rand',
'posts_per_page' => 3,
'paged' => $paged,
//'ignore_sticky_posts' => 1,
'tax_query' => array(
array(
'taxonomy' => 'project-category',
'field' => 'slug',
'terms' => $category->slug,
),
),
);
$query_thema = new WP_Query( $args_thema ); if ( $query_thema->have_posts() ) : ?>
<ul class="">
<?php $i = 0; while ( $query_thema->have_posts() ) : $query_thema->the_post(); $i++; // echo $i; ?>
<li class="">
<a class="url uid" href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php else : ?>
<?php endif; wp_reset_postdata(); ?>
</div>
</div>
<?php } ?>
Sorry, it was an extra function which sets the archive posts to 99.
Istead of is_post_type_archive I used is_archive
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 have the loop below that is doing the following:
Looping through all my custom taxonomy terms and seeing if the
checkbox "category on homepage" is selected, it shuffles those
selected.
It then loops through those terms that are selected and pulls out 1 custom post at random for each of those terms.
It then outputs a box with the term name and an image from the post selected.
The problem I'm having is that posts are tagged with multiple terms, so chances are it will select the same post across the different terms.
How can I ensure that the posts that are selected are unique?
<?php $terms = get_terms('project-categories', array(
'orderby' => 'rand',
'hide_empty' => 1,
)
);
shuffle($terms);
foreach ( $terms as $term ) {
if(get_field('category_on_homepage', 'project-categories_' . $term->term_id)) {
if (in_array('Yes', get_field('category_on_homepage', 'project-categories_' . $term->term_id))) {
$termid = $term->term_id;
$termname = $term->name;
$termslug = $term->slug;
$args = array(
'posts_per_page' => 1,
'orderby' => 'rand',
'post_type' => 'projects',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'project-categories',
'field' => 'id',
'terms' => $termid
)
)
);
$projects = get_posts( $args );
foreach ( $projects as $post ) {
setup_postdata($post); ?>
<a class="service" href="<?php echo $termslug; ?>">
<span class="service-image" style="background-image:url('<?php echo the_field('thumb_image'); ?>');"></span>
<span class="service-title"><?php echo $termname; ?></span>
</a>
<?php }
wp_reset_postdata();
}
}
} ?>
Build an array of used post ids, then use the post__not_in query parameter to exclude the used posts from future queries.
<?php
$terms = get_terms('project-categories', array(
'orderby' => 'rand',
'hide_empty' => 1
)
);
$used_posts = array();
shuffle($terms);
foreach ( $terms as $term ) {
if(get_field('category_on_homepage', 'project-categories_' . $term->term_id)) {
if (in_array('Yes', get_field('category_on_homepage', 'project-categories_' . $term->term_id))) {
$termid = $term->term_id;
$termname = $term->name;
$termslug = $term->slug;
$args = array(
'posts_per_page' => 1,
'orderby' => 'rand',
'post_type' => 'projects',
'post_status' => 'publish',
'post__not_in' => $used_posts,
'tax_query' => array(
array(
'taxonomy' => 'project-categories',
'field' => 'id',
'terms' => $termid
)
)
);
$projects = get_posts( $args );
foreach ( $projects as $post ) {
setup_postdata($post);
array_push($used_posts, $post->ID);
?>
<a class="service" href="<?php echo $termslug; ?>">
<span class="service-image" style="background-image:url('<?php echo the_field('thumb_image'); ?>');"></span>
<span class="service-title"><?php echo $termname; ?></span>
</a>
<?php
}
wp_reset_postdata();
}
}
}
?>
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(); ?>