I can't seem to work out why my ACF fields will not display below my woo-commerce products on the featured page when they display fine above them?
Products
<div id="post-load" class="row products">
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'nopaging' => true,
'tax_query' => array(
array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'featured',
'operator' => 'IN'
),
),
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product; ?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile; ?>
</div>
ACF Example (This works fine if placed above the products)
<?php
$features = get_field('features');
if( $features ): ?>
<div class="col-md-12"><h3 class="txtc"><?php echo esc_html( $features['title'] ); ?></h3></div>
<?php endif; ?>
Related
Im trying to display specific product categories in an ACF repeater field.
This keeps outputting all product categories.
How do I go to output single category per repeater field?
<?php if( have_rows('product_categories') ): ?>
<ul class="products">
<?php while( have_rows('product_categories') ): the_row(); ?>
<?php
$product_category_ids = get_sub_field('project_category');
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'tax_query' => array(
'taxonomy' => 'product_cat',
'terms' => $product_category_ids
),
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' ); ?>
<?php
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
<?php endwhile; ?>
</ul>
This worked for me:
<?php if( have_rows('product_categories') ): ?>
<ul class="products">
<?php while( have_rows('product_categories') ): the_row(); ?>
<?php
$product_cats = get_sub_field('product_category');
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => '12',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $product_cats,
'operator' => 'IN',
'field' => 'slug'
),
)
);
$loop = new WP_Query($args);
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
<?php endwhile; ?>
</ul>
I'd like to exclude the category of my current post from the loop. Pretty easy usually, this time it doesn't work and I can't figure out what's wrong here.
Here's my page'code:
$postid = get_the_ID(); // curret product ID
<section class="related products">
<?php
$args = array(
'post__not_in' => array($postid), // Exclude displayed product
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => '6',
'cat' => '-33' // Exclude cat
);
$related_products = new WP_Query($args);
?>
<h2><?php esc_html_e( 'Related products' ); ?></h2>
<div class="owl-carousel rigid-owl-carousel" >
<?php if( $related_products->have_posts() ) {
while( $related_products->have_posts() ) : $related_products->the_post();
wc_get_template_part( 'content', 'product' );
endwhile; }
?>
</section>
End of page
<?php
wp_reset_postdata();
?>
It shows all products (except the displayed one, which is correct).
Do you have any suggestion?
Try with the following additional tax_query, as product categories are a custom taxonomy:
<?php
$related_products = new WP_Query( array(
'post__not_in' => array( get_the_ID() ), // Exclude displayed product
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => '6',
'tax_query' => array( array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => array( 33 ), // HERE the product category to exclude
'operator' => 'NOT IN',
) ),
) );
if( $related_products->have_posts() ) : ?>
<h2><?php esc_html_e( 'Related products' ); ?></h2>
<div class="owl-carousel rigid-owl-carousel" >
<?php
while( $related_products->have_posts() ) : $related_products->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
wp_reset_postdata();
?>
</div>
<?php endif; ?>
You can get current post's category using get_the_category
and you can exclude categories using category__not_in in your argument.
So your argument should be like bellow
$args = array(
'post__not_in' => array($postid), // Exclude displayed product
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => '6',
'category__not_in' => get_the_category(get_the_ID())//exclude category of current post
);
Try this then let me know the result. Thanks
I would like to get a pager in my page, can i create a pager with a for each or while without parameter like "posts_per_page" ?
I get term children of my taxonomy and i would like to display 3 by 3
My code :
$childGalerie = get_term_children(15,'wpmf-category');
foreach ( $childGalerie as $oneGalerie ) :
$maGalerie = get_term($oneGalerie, 'wpmf-category');
?>
<h2 class="info-galerie"><span></span><?php echo $maGalerie->name; > </h2>
<div class="galerie-photo">
<?php
global $post;
$args = array(
'posts_per_page' => -1,
'post_type' => 'attachment',
// 'post_status' => 'inherit',
'tax_query' => array(
array(
'taxonomy' => 'wpmf-category',
'field' => 'term_id',
'terms' => $oneGalerie
),
),
);
$media = get_posts( $args );
foreach ( $media as $post ) : setup_postdata( $post );
?>
<div class="wrapper-galerie">
<?php echo wp_get_attachment_image( $media->ID, 'galerie->photo' ); ?>
</div>
<?php
endforeach;
wp_reset_postdata();
?>
</div>
<?php
endforeach;
Can you help me please , ty :D
I'm trying to display a post type’s categories and output the posts within that category, but I have another taxonomy that I want to exclude. This code is showing all Taxonomies and Terms registered to the specific post type.
The second taxonomy that I am trying to exclude is named "manufacturer" and is registered to multiple post types. I have tried doing 'operator'=>'NOT EXISTS' as well as what you see below, with the if...continue but nothing seems to work !!
<?php
$post_type = 'equipment';
$taxonomies = get_object_taxonomies( array( 'post_type' => $post_type ) );
foreach( $taxonomies as $taxonomy ) :
$terms = get_terms( $taxonomy );
foreach( $terms as $term ):
if ( $term->slug == 'manufacturer' )
continue;
?>
<div class="row">
<div class="col-xs-12">
<h2><?php echo $term->name; ?></h2>
</div>
<div class="col-xs-12">
<?php
$args = array(
'post_type' => $post_type,
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'slug',
'terms' => $term->slug,
)
)
);
$posts = new WP_Query($args);
if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post(); ?>
<h4><?php echo get_the_title(); ?></h4>
<?php endwhile; endif; ?>
</div>
</div>
<?php endforeach;
endforeach; ?>
)
$args = array(
'post_type' => array('equipment'),
'posts_per_page' => -1,
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'manufacturer',
'field' => 'slug',
'operator' => 'NOT IN',
),
),
);
$query = new WP_Query( $args );
I have created a custom taxonomy for a custom post. Now i want to show all post related to a particular term. Suppose i have two terms term1 and term2. When click on term1 then all post related to term1 will show and post , that is related to term2 will not show. But now when i click term1 then post related to term1 and term2 are showing at a time. I have wrote the following code to taxonomy.php template.
<div class="main-content">
<?php
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$the_query = new WP_Query( array(
'post_type' => array('newsbox_post'),
'tax_query' => array(
'taxonomy' => $term->taxonomy,
'field' => 'slug',
'terms' => $term->name,
),
) );
?>
<?php if ($the_query->have_posts()) : while ($the_query->have_posts()) : $the_query->the_post(); ?>
<div class="post">
<?php the_title(); ?>
<?php the_content(); ?>
<?php echo get_the_term_list( $post->ID, $term->taxonomy, 'People: ', ', ' ); ?>
<hr />
</div>
<?php
endwhile;
wp_reset_postdata();
else:?>
<h3><?php _e('404 Error: Not Found'); ?></h3>
<?php
endif;
?>
</div>
Please tell me , how can i achieve that.
I have got the answer .
$the_query = new WP_Query( array(
'post_type' => array('newsbox_post'),
'tax_query' => array(
array(
'taxonomy' => $term->taxonomy,
'field' => 'slug',
'terms' => $term->slug,
),
),
) );
You should use slug instead of name like following code:-
$the_query = new WP_Query( array(
'post_type' => array('newsbox_post'),
'tax_query' => array(
array(
'taxonomy' => $term->taxonomy,
'field' => 'slug',
'terms' => $term->slug,
),
),
) );
Hope this will help you.