I want to display the product in the tab category.
I want it to display 4 items in each tab but it currently displays all product.
Do you have a solution to help me with this?
I do not want the products to be displayed in blocks, it should be displayed in tabular.
Currently displays all products from all categories but does not display each product as a specific category.
This is my code.
<div class="container pt-5">
<div class="text-center mb-4">
<h2 class="section-title px-5"><span class="px-2">Category products</span></h2>
</div>
<ul class="nav nav-tabs" id="myTab" role="tablist">
<?php
$categories = get_terms( array(
'taxonomy' => 'product_cat',
'hide_empty' => 1,
) );
foreach ( $categories as $key => $cat ) { ?>
<li class="nav-item" role="presentation">
<a class="nav-link " id="<?php echo $cat->slug; ?>" data-toggle="tab" href="#<?php echo $cat->slug; ?>" role="tab" aria-controls="<?php echo $cat->slug; ?>" aria-selected="true"><?php echo $cat->name; ?></a>
</li>
<?php } ?>
</ul>
<?php foreach ( $categories as $key => $cat ) {
$args = array(
'post_type' => 'product',
'posts_per_page' => 4,
'product_cat' => $cat->slug,
'hide_empty' => 1,
'orderby' => 'name',
'order' => 'ASC'
);
?>
<div class="tab-content" id="<?php echo $cat->slug; ?>">
<?php
$products = new WP_Query( $args );
if( $products->have_posts() ) : while ( $products->have_posts() ) : $products->the_post(); ?>
<div class="tab-pane<?php echo $cat->slug; ?>" id="<?php echo $cat->slug; ?>" role="tabpanel" aria-labelledby="home-tab">
<div class="container pt-2">
<div class="row px-xl-5 pb-3">
<div class="col-lg-3 col-md-6 col-sm-12 pb-1">
<div class="card product-item border-0 mb-4">
<div class="card-header product-img position-relative overflow-hidden bg-transparent border p-0">
<?php the_post_thumbnail('', array('class' => 'img-fluid w-100')); ?>
</div>
<div class="card-body border-left border-right text-center p-0 pt-4 pb-3">
<h6 class="text-truncate mb-3"><?php the_title(); ?></h6>
<div class="d-flex justify-content-center">
<h6><?php echo "". " " . get_woocommerce_currency_symbol().$product->get_price(); ?></h6>
</div>
</div>
<div class="card-footer d-flex justify-content-between bg-light border">
<i class="fas fa-eye text-primary mr-1"></i>View Detail
<i class="fas fa-shopping-cart text-primary mr-1"></i> Add to Card
</div>
</div>
</div>
</div>
</div>
</div>
<?php endwhile;
wp_reset_postdata(); endif; ?>
</div>
<?php } ?>
</div>
Your loop was off a little bit, you also used the attribute ID multiple times. This should work for you.
<div class="container pt-5">
<div class="text-center mb-4">
<h2 class="section-title px-5"><span class="px-2">Category products</span></h2>
</div>
<ul class="nav nav-tabs" id="myTab" role="tablist">
<?php
$product_cats = get_terms(
array(
'taxonomy' => 'product_cat',
'hide_empty' => 1,
)
);
foreach ( $product_cats as $key => $product_cat ) {
?>
<li class="nav-item" role="presentation">
<a class="nav-link " id="<?php echo esc_attr( $product_cat->slug ); ?>-tab" data-toggle="tab" href="#<?php echo esc_attr( $product_cat->slug ); ?>" role="tab" aria-controls="<?php echo esc_attr( $product_cat->slug ); ?>" aria-selected="false"><?php echo esc_html( $product_cat->name ); ?></a>
</li>
<?php } ?>
</ul>
<div class="tab-content">
<?php
$c = 0;
foreach ( $product_cats as $key => $product_cat ) {
$args = array(
'post_type' => 'product',
'posts_per_page' => 4,
'product_cat' => $product_cat->slug,
'hide_empty' => 1,
'orderby' => 'name',
'order' => 'ASC',
);
?>
<div class="tab-pane <?php echo ( 0 === $c ) ? 'show active' : 'fade'; ?>" id="<?php echo esc_attr( $product_cat->slug ); ?>" role="tabpanel" aria-labelledby="<?php echo esc_attr( $product_cat->slug ); ?>-tab"
<div class="container pt-2">
<div class="row px-xl-5 pb-3">
<?php
$products = new WP_Query( $args );
if ( $products->have_posts() ) :
while ( $products->have_posts() ) :
$products->the_post();
?>
<div class="col-lg-3 col-md-6 col-sm-12 pb-1">
<div class="card product-item border-0 mb-4">
<div class="card-header product-img position-relative overflow-hidden bg-transparent border p-0">
<?php the_post_thumbnail( '', array( 'class' => 'img-fluid w-100' ) ); ?>
</div>
<div class="card-body border-left border-right text-center p-0 pt-4 pb-3">
<h6 class="text-truncate mb-3"><?php the_title(); ?></h6>
<div class="d-flex justify-content-center">
<h6><?php echo '' . ' ' . get_woocommerce_currency_symbol() . esc_attr( $product->get_price() ); ?></h6>
</div>
</div>
<div class="card-footer d-flex justify-content-between bg-light border">
<i class="fas fa-eye text-primary mr-1"></i>View Detail
<i class="fas fa-shopping-cart text-primary mr-1"></i> Add to Card
</div>
</div>
</div>
<?php
endwhile;
?>
<?php
wp_reset_postdata();
endif;
?>
</div>
</div>
<?php
$c++;
}
?>
</div>
</div>
Related
I created a custom blog template in my WPLMS Wordpress theme, with wp-query. But I'm unable to show the pagination. I tried to build a shortcode its working fine but in the last im unable to show my pagination.
//blog section 2
add_shortcode('da-blog', 'da_blog_post');
function da_blog_post($attr, $content = null){
$attributes = extract( shortcode_atts(array(
'title' => 'blog post',
'subtitle' => 'latest news',
), $attr) );
ob_start(); ?>
<section class="exthree_blog">
<div class="container">
<div class="row">
<div class="col-md-9">
<div class="all_exthree_blogs">
<div class="row">
<?php
$blogcontent = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => 6,
'category__not_in' => array( 75 )
));
?>
<?php while($blogcontent->have_posts()) : $blogcontent->the_post(); ?>
<div class="col-md-4">
<div class="single_exthree_blog">
<div class="exthree_box">
<div class="exthree_box_img">
<!-- <img src="img/shutterstock_770159185.jpg" class="img-responsive"> -->
<a href="<?php echo get_permalink() ?>">
<?php if(has_post_thumbnail(get_the_ID())){ ?>
<?php echo get_the_post_thumbnail(get_the_ID(),'medium'); ?>
<?php }else{
$image = vibe_get_option('default_course_avatar');
?>
<img src="<?php echo vibe_sanitizer($image,'url'); ?>" />
<?php
}
$name = get_the_author_meta( 'display_name' );
?>
</a>
</div>
<div class="exthree_box_fortag">
</div>
<div class="boxxx">
<div class="exthree_box_title">
<h4><?php echo get_the_title(); ?></h4>
</div>
<div class="exthree_box_categor_list">
<?php echo get_the_category_list(); ?>
</div>
<div class="exthree_box_desc">
<p><?php echo get_the_excerpt(); ?></p>
</div>
<div class="exthree_box_button">
Continue<img src="http://localhost/gostudent/wp-content/uploads/2020/02/Layer-2.png">
</div>
</div>
</div>
</div>
</div>
<?php endwhile; ?>
<?php echo "helooooooooooooooo"; ?>
<?php $big = 999999999; // need an unlikely integer
$links = paginate_links(array(
'base' => str_replace($big, '%#%', get_pagenum_link($big)),
'format' => '?paged=%#%',
'current' => max(1, get_query_var('paged')),
'total' => $next_the_query->max_num_pages,
"type" => "list"
));
echo $links; ?>
</div>
</div>
</div>
</div>
</div>
</section>
<?php return ob_get_clean();
wp_reset_postdata();
}
I used bootstrap 4 to design the blog page. Design is well but when I am using wp_posts design not working properly.
<?php
global $post;
$events = array(
'post_type' => 'post',
'post_status' => 'publish',
'category_name' => 'events',
'posts_per_page' => 4,
);
$arr_posts = new WP_Query( $events );
$myposts = get_posts( $events );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<div class="row single-event">
<div class="col-6 p-0 m-0">
<?php
if ( has_post_thumbnail() ) :
the_post_thumbnail();
endif;
?>
</div>
<div class="col-6 event-desc">
<?php the_title() ?>
<p class="exerpt">
<?php if ( is_category() || is_archive() ) {
echo excerpt(30);
} else {
echo content(30);
}
?>
</p>
<p class="date">Event Date: <span><?php echo get_the_date('F j, Y'); ?></span></p>
Learn More..
</div>
</div>
<?php endforeach;
wp_reset_postdata();
?>
Here is my site https://rccsl.com/events/
Please help. Thanks in advance.
Please use below code
<?php
global $post;
$events = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 4,
);
$arr_posts = new WP_Query( $events );
$myposts = get_posts( $events );
foreach ( $myposts as $post ) { setup_postdata( $post )?>
<div class="row single-event">
<div class="col-6 p-0 m-0">
<?php
if ( has_post_thumbnail() ) :
the_post_thumbnail();
endif;
?>
</div>
<div class="col-6 event-desc">
<?php the_title() ?>
<p class="exerpt">
<?php if ( is_category() || is_archive() ) {
echo "test";
} else {
echo "test";
}
?>
</p>
<p class="date">Event Date: <span><?php echo get_the_date('F j, Y'); ?></span></p>
Learn More..
</div>
</div>
<?php } ?>
It seems that the html code is broken when it's output. If you inspect your website with the developers tools you will see that instead of having an output of:
<div class="row single-event">
<div class="col-6 p-0 m-0"></div>
<div class="col-6 p-0 m-0"></div>
</div>
<div class="row single-event">
<div class="col-6 p-0 m-0"></div>
<div class="col-6 p-0 m-0"></div>
</div>
<div class="row single-event">
<div class="col-6 p-0 m-0"></div>
<div class="col-6 p-0 m-0"></div>
</div>
You have as a result:
<div class="row single-event">
<div class="col-6 p-0 m-0"></div>
<div class="col-6 p-0 m-0"></div>
<div class="row single-event">
<div class="col-6 p-0 m-0"></div>
<div class="col-6 p-0 m-0"></div>
</div>
<div class="row single-event">
<div class="col-6 p-0 m-0"></div>
<div class="col-6 p-0 m-0"></div>
</div>
</div>
First of all add a semicolon (;) after the_title()
the_title();
Then try to replace
<p class="exerpt">
<?php if ( is_category() || is_archive() ) {
echo excerpt(30);
} else {
echo content(30);
}
?>
</p>
With:
<div class="exerpt">
<?php if ( is_category() || is_archive() ) {
echo excerpt(30);
} else {
echo content(30);
}
?>
</div>
I have a custom post type called "case_studies" and it has 3 categories: "seo", "website-design" and "facebook-advertising".
I have several posts in the main page, and for each post I want to output what categories the post has.
For example, the latest post has a category of "seo" and "facebook-advertising", and I want it to be displayed in the page.
I have tried the get_the_terms(); function but I think I am using it incorrectly.
Here is the code of the page:
<div class="container">
<!--
<div class="category_container">
<p class="category_item" id="all">All</p>
<p class="category_item" id="website">Websites</p>
<p class="category_item" id="facebook">Facebook Ads</p>
<p class="category_item" id="seo">SEO</p>
</div>
-->
<div class="row d-flex">
<?php
$args1 = array( 'post_type' => array('case_studies'), 'order' => 'DESC', 'posts_per_page' => 30, 'orderby' => 'date' );
$loop = new WP_Query( $args1 );
while ( $loop->have_posts() ) {
$loop->the_post();
$feat_image = wp_get_attachment_url( get_post_thumbnail_id($loop->ID));
?>
<div class="col-sm-4">
<div class="case-study-content">
<a href="<?php the_permalink() ?>" class="blog_blocks">
<div class="b_image">
<img src="<?php echo get_the_post_thumbnail_url(); ?>"/>
</div>
<div class="b_h_sec">
<h2><?php the_title(); ?></h2>
<p><?php echo wp_strip_all_tags( get_the_excerpt(), true ); ?></p>
<span class="r_m">Read More</span>
</div>
</a>
</div>
</div>
<?php } ?>
<?php wp_reset_postdata(); ?>
</div>
</div>
Here is how the output looks right now and where I want categories to be displayed.
I'm creating a loop in WordPress for a fancybox gallery. This means that each group of images needs to have the same rel identifier (for example rel="gallery1"). But since this is a nested loop, in other words, it's part of another loop, I need the next group of images in the loop to have a rel identifier different from the ones before/after (ex. rel="gallery2", rel="gallery3", etc.).
I've tried putting next the the word gallery, but it just loops and gives every object another number instead of keeping the same number for each item in the grouping.
I'm not sure if what I'm explaining is making sense. Here's a basic idea of what I'm trying to have.
{gallery of images 1}
<a href="image" rel="gallery1">
<img src="path">
</a>
<a href="image" rel="gallery1">
<img src="path">
</a>
<a href="image" rel=gallery1">
<img src="path">
</a>
{gallery of images2 }
<a href="image" rel="gallery2">
<img src="path">
</a>
<a href="image rel="gallery2">
<img src="path">
</a>
etc., etc.
The page itself is displaying a gallery of post thumbnails (featured images) from a custom post type. Clicking on a gallery item (representing one item in the custom post type) opens a Bootstrap modal window. In that modal window is a group of images related to that original gallery item (post type item). Clicking on one of those images opens a fancybox, but should only cycle through the images for that gallery item. Crazy enough?
The rest of the page is working perfectly. Just having trouble with this rel numbering thing.
I had hesitated posting all the code because it's so long, but if it's helpful, here you go:
// Define the query
$args = array(
'post_type' => 'pixieportfolio',
'post_status' => 'publish',
'orderby' => 'menu_order',
'meta_query' => array(
array(
'key' => 'featured_portfolio_item',
'value' => '1',
'compare' => '=',
)
),
'meta_key' => '_thumbnail_id',
'posts_per_page' => 999
);
$query = new WP_Query( $args );
$count = 0;
// Carousel // ?>
<div id="featuredCarousel" class="carousel slide" data-ride="carousel" data-pause="hover">
<!-- Indicators -->
<ol class="carousel-indicators">
<?php while($query->have_posts()): $query->the_post(); ?>
<li <?php if($count == 0){ echo 'class="active"';} ?> data-target="#featuredCarousel" data-slide-to="<?php echo $count++; ?>"></li>
<?php endwhile; ?>
</ol>
<div class="carousel-inner" role="listbox">
<?php $count = 0;
while ($query->have_posts()) : $query->the_post();
$count++;
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'pp-home-portfolio' );
$url = $thumb['0'];
$values = get_field('tech_choices');
$terms = get_the_terms($post->ID, 'post_tag');
$custom = get_post_custom($post->ID);
$caption = get_field('caption', '');
$client = get_field('client', '');
$project_url = get_field('project_url', '');
$date = get_field('date', ''); ?>
<!-- Wrapper for slides -->
<div class="item <?php if ($count == 1) { echo 'active';} ?>" data-slide-number="<?php echo $count++; ?>" id="<? the_ID(); ?>">
<div class="item-image" style="background-image: url(<?=$url?>);" title="<?php the_title() ?>" data-toggle="modal" data-target="#myModal<?php echo $count; ?>"></div>
<div class="project-details">
<div class="project-detail-border shadow-effect"></div>
<div class="project-detail">
<div class="top-detail">
<h4><?php the_title(); ?></h4>
<div class="short-description">
<?php $content = get_the_content();
echo wp_trim_words( $content , '20' );
?>
</div>
</div>
<table class="table project">
<tr>
<td valign="middle" class="fifty">
<?php $terms_as_text = strip_tags( get_the_term_list( $wp_query->post->ID, 'pixie-portfolio-categories', '', ', ', '' ) );
echo $terms_as_text;?>
</td>
<td valign="middle" class="fifty">
<?php if( $values ) {
if(in_array('WordPress', $values )){ ?>
<i class="fa fa-wordpress fa-2x" data-toggle="tooltip" title="Wordpress"></i>
<?php } if(in_array('HTML-5', $values )){ ?>
<i class="fa fa-html5 fa-2x" data-toggle="tooltip" title="HTML-5"></i>
<?php } if(in_array('CSS-3', $values )){ ?>
<i class="fa fa-css3 fa-2x" data-toggle="tooltip" title="CSS-3"></i>
<?php } if(in_array('Sportswear', $values )){ ?>
<i class="fa fa-bicycle fa-2x" data-toggle="tooltip" title="Sportswear"></i>
<?php } if(in_array('Adobe', $values )){ ?>
<i class="fa fa-paint-brush fa-2x" data-toggle="tooltip" title="Adobe"></i>
<?php } if(in_array('Code', $values )){ ?>
<i class="fa fa-code fa-2x" data-toggle="tooltip" title="Code"></i>
<?php } if(in_array('Ecommerce', $values )) { ?>
<i class="fa fa-database fa-2x" data-toggle="tooltip" title="MySQL"></i>
<?php }
} else {} ?>
</td>
</tr>
<tr>
<td colspan="2" class="more-info" data-toggle="modal" data-target="#myModal<?php echo $count; ?>">
<div class="view-project">View Project</div>
</td>
</tr>
</table>
</div>
</div>
</div>
<!-- Modal -->
<div id="myModal<?php echo $count; ?>" class="modal fade" role="dialog" aria-labelledby="myModal<?php echo $count; ?>Label">
<div class="modal-dialog modal-lg" role="document">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h2 class="modal-title"><?php the_title() ?></h2>
<span>
<?php if( $terms ): ?>
<ul>
<?php foreach( $terms as $term ): ?>
<li><?php echo $term->name; ?></li>
<?php $icon = get_field('creative_icon', $term->taxonomy . '_' . $term->term_id); echo $icon; ?>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</span>
</div>
<div class="modal-body">
<div class="container-fluid">
<div class="row">
<div class="col-md-8">
<div>
<p class="post-meta gallery-list">
<?php if( $client ): ?>
<span class="pixie-date">
<span class="title">Client:</span> <?=$client?>
</span>
<?php endif;
if ( $date ): ?>
<span class="post-cats">
<span class="title">Date:</span> <?=$date?>
</span>
<?php endif;
if ( $values ) { ?>
<span class="post-comments">
<?php $values = get_field('tech_choices'); ?>
<span class="title">Tech used in this project:</span>
<?php if(in_array('WordPress', $values )){ ?>
<i class="fa fa-wordpress fa-2x" data-toggle="tooltip" data-placement="bottom" title="WordPress"></i>
<? }
if(in_array('HTML-5', $values )){ ?>
<i class="fa fa-html5 fa-2x" data-toggle="tooltip" data-placement="bottom" title="HTML5"></i>
<? }
if(in_array('CSS-3', $values )){ ?>
<i class="fa fa-css3 fa-2x" data-toggle="tooltip" data-placement="bottom" title="CSS3"></i>
<? }
if(in_array('Sportswear', $values )){ ?>
<i class="fa fa-bicycle fa-2x" data-toggle="tooltip" data-placement="bottom" title="Sportswear"></i>
<? }
if(in_array('Adobe', $values )){ ?>
<i class="fa fa-paint-brush fa-2x" data-toggle="tooltip" data-placement="bottom" title="Illustrator and/or Photoshop"></i>
<? }
if(in_array('Code', $values )){ ?>
<i class="fa fa-code fa-2x" data-toggle="tooltip" data-placement="bottom" title="PHP, JQuery, etc."></i>
<? }
if(in_array('Ecommerce', $values )){ ?>
<i class="fa fa-shopping-cart fa-2x" data-toggle="tooltip" data-placement="bottom" title="ECommerce"></i>
<? }
if(in_array('MySQL', $values )){ ?>
<i class="fa fa-database fa-2x" data-toggle="tooltip" data-placement="bottom" title="MySQL"></i>
<? } ?>
</span>
<?php } ?>
</p>
</div>
<?php echo the_content(); ?>
<blockquote class="testimonial">
QUOTE GOES HERE
</blockquote>
</div>
<div class="col-md-4">
<div class="gallery">
<?php $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'darwin-modal-main' );
$url = $thumb['0'];
$thumb2 = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'large' );
$url2 = $thumb2['0'];
$c = 0; ?>
<a href="<?=$url2?>" class="group" id="port_gal" rel="group<?php echo $c; ?>">
<img src="<?=$url?>" width="300" height="300">
</a>
<?php $images = get_field('gallery_images');
if( $images ) {
foreach( $images as $image ) {
?>
<a href="<?php echo $image['sizes']['large'] ?>" class="group" id="port_gal" rel="group<?php echo $c; ?>">
<img src="<?php echo $image['sizes']['darwin-modal-thumb'] ?>" alt="<?php $image['alt'] ?>" />
</a>
<?php }
} ?>
</div>
<div class="website-button">
<?php if ( $project_url ): ?>
<a class="btn btn-default" href="<?=$project_url?>" target="_blank" role="button">Visit Website</a>
<?php endif; ?>
</div>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
</div>
</div>
</div>
<?php endwhile; ?>
</div>
<?php wp_reset_query(); ?>
</div>
I am trying to get the post thumbnail image for my carousel thumbnail indicators which are outside of the loop.
Currently it's setup with image placeholders but I need each thumb to represent the currently selected post.
<?php
$items = new WP_Query(array(
'post__in' => get_option('sticky_posts'),
'caller_get_posts' => 1,
'posts_per_page' => 10,
'meta_key' => '_thumbnail_id'
));
$count = $items->found_posts;
?>
<div id="myCarousel" class="carousel slide" data-ride="carousel">
<!-- Wrapper for slides -->
<div class="carousel-inner">
<?php
$ctr = 0;
while ( $items->have_posts() ) :
$items->the_post();
$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
$custom = get_post_custom($post->ID);
$link = $custom["more-link"][0];
$class = $ctr == 0 ? ' active' : '';
?>
<div class="item<?php echo $class; ?>" id="<? the_ID(); ?>">
<?php the_post_thumbnail( 'full', array (
'class' => 'img-responsive'
)); ?>
<div class="carousel-caption">
<h3><a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a></h3>
</div>
</div>
<!-- End Item -->
<?php $ctr++;
endwhile; ?>
</div>
<!-- End Carousel Inner -->
<div class="thumbs">
<div class="row">
<div class="col-md-2 active item" data-target="#myCarousel" data-slide-to="0"><img src="http://placehold.it/1200x440/999999/cccccc" class="img-responsive"></div>
<?php for($num = 1; $num < $count; $num++){ ?>
<div class="col-md-2 item" data-target="#myCarousel" data-slide-to="<?php echo $num; ?>"><img src="http://placehold.it/1200x440/999999/cccccc" class="img-responsive"></div>
<?php } ?>
</div>
</div>
</div>
<!-- End Carousel -->
Instead of:
<?php for($num = 1; $num < $count; $num++){ ?>
<div class="col-md-2 item" data-target="#myCarousel" data-slide-to="<?php echo $num; ?>">
<a href="#">
<img src="http://placehold.it/1200x440/999999/cccccc" class="img-responsive">
</a>
</div>
<?php } ?>
try using:
<?php $counter2 = 0; ?>
<?php while ( $items->have_posts() ) : $items->the_post(); ?>
<?php $counter2++; ?>
<div class="col-md-2 item <?php echo $counter2 == 1 ? 'active' : ''; ?>" data-target="#myCarousel" data-slide-to="<?php echo $counter2; ?>">
<a href="#">
<?php
the_post_thumbnail('thumbnail', array(
'class' => 'img-responsive'
));
?>
</a>
</div>
<?php endwhile; ?>
This will display it with your markup, but with a thumbnail image instead of the placeholder.