How to can query woocommerce product in bootstrap modal? - wordpress

I've created a custom template then I've a query woocommerce product in that template using while() method. That's working fine. But I've added one bootstrap v3 modal in that query. That bootstrap modal using because if I have variable product and show Select Option for variation selection, then when user click 'select option', then, open bootstrap modal, I've successfully that work, but problem when open that modal show only one (latest) product, example: if I click select option for "Food one" then open modal and show latest last product, like "food one" but if I click "food two" then open modal and show same product in that modal like "food one".
Note: I have added that modal in first while() method. You can check that problem on our live server/site https://www.theitaliankitchen.com.bd/m/
You can check this screenshots
I have clicked "Agnolotti alla Piemontese" product
then open modal
Then I've clicked "Mascarpone" Product
Then Open same product
But i want to show which product I've clicked show only that product information in modal.
Here is my full custom template codes
<?php
/*
Template Name: Mobile Version
*/
get_header(); global $product;
$categorydesign = get_option( 'custom_category_options', array() );
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
?>
<?php
/**
* tokoo_before_main_content hook
*
* #hooked tokoo_wrapper_start - 10 (outputs opening divs for the content)
*/
do_action( 'tokoo_before_main_content' );
?>
<div class="shopmobile-version-area ">
<div class="shop-mobile-version column ">
<div class="msite-title-area fix">
<h2><?php echo bloginfo('name'); ?></h2>
</div>
<!-- Category Design Area-->
<div class="category-design-area fix">
<?php
if ( ! empty( $categorydesign['category_designgroup'] ) ) :
foreach ( $categorydesign['category_designgroup'] as $catdesign ) : ?>
<?php
$category_info = $catdesign['category_design_name'];
$cat_name = get_term_by('slug', $category_info, 'product_cat'); // get name by slug
$cat_link = get_term_link($cat_name->term_id); // get term link by id
$icon_link = $catdesign['category_design_icon'];
?>
<!--Single Category Item-->
<div class="single-category-item">
<img src="<?php echo $icon_link;?>" alt="<?php echo $cat_name->name;?>">
<h4> <?php echo $cat_name->name;?> </h4>
</div><!--/ Single Category Item-->
<?php endforeach; endif; ?>
</div><!--/ Category Design Area-->
<div class="mproduct-area fix">
<?php
$mproduct = new WP_Query(array(
'post_type' => 'product',
'posts_per_page'=> 10,
));
if($mproduct->have_posts()) : while($mproduct->have_posts()) : $mproduct->the_post(); ?>
<!--Single Product for Mobile Page-->
<div class="msingle-product fix">
<div class="msingle-product-image">
<a href="<?php the_permalink(); ?>">
<?php if(has_post_thumbnail( get_the_ID())){
echo get_the_post_thumbnail( get_the_ID(), 'com_product_img' );
} else {
echo '<img src="' . woocommerce_placeholder_img_src() . '" alt="Placeholder" />';
}
?></a>
</div>
<div class="mproduct-price">
<h2><strong>Price : </strong> <?php echo $product->get_price_html(); ?></h2>
</div>
<div class="mproduct-name">
<h2><?php echo esc_attr( $product->get_title() ) ?></h2>
</div>
<div class="mproduct-add-to-cart">
<?php woocommerce_template_loop_add_to_cart(); ?>
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel"><?php the_title(); ?></h4>
</div>
<div class="modal-body">
<div class="msingle-product-image product_img_model">
<a href="<?php the_permalink(); ?>">
<?php if(has_post_thumbnail( get_the_ID())){
echo get_the_post_thumbnail( get_the_ID(), 'com_product_img' );
} else {
echo '<img src="' . woocommerce_placeholder_img_src() . '" alt="Placeholder" />';
}
?></a>
</div>
<div class="mproduct-price">
<h2><strong>Price : </strong> <?php echo $product->get_price_html(); ?></h2>
</div>
<div class="product_variable_list">
<?php wc_get_template_part('content', 'single-product'); ?>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Continue Shipping</button>
</div>
</div>
</div>
</div>
</div>
</div><!--/ Single Product for Mobile Page-->
<?php endwhile; endif;?>
</div>
</div>
</div>
<?php
/**
* tokoo_after_main_content hook
*
* #hooked tokoo_wrapper_end - 10 (outputs closing divs for the content)
*/
do_action( 'tokoo_after_main_content' );
?>
<?php get_footer(); ?>
You can watch this video for good understanding https://youtu.be/6YbYrp3ALBg

changes this script in app.js file
<script>
$('.shop-mobile-version').find('.msingle-product .button.product_type_variable.add_to_cart_button').on('click', function(){
$(this).next('.modal').modal().css('display', 'block');
//$('#myModal').modal().css('display', 'block');
return false;
});
</script>

Related

Custom Taxonomy List Wordpress

I'm really a beginner for creating and displaying custom tax on WordPress.
I created a custom Genre taxonomy and I'm displaying this with this:
<?php $terms = get_the_terms( get_the_ID(), 'movie_genre' ); if( $terms ): ?>
<!-- Movie Genre -->
<div class="slideout-heading">
<div class="d-inline-block">
<h5><?php esc_html_e( 'Genre'); ?></h5>
</div>
</div>
<div class="row">
<div class="carousel mb-3 w-100" data-flickity='{ "pageDots": false, "imagesLoaded": true, "prevNextButtons": true, "cellAlign": "left", "contain": true, "groupCells": true }'>
<?php foreach( $terms as $term ): ?>
<?php
$attachment_id = get_field('taxonomy_image', $term);
$size = "wp-img-square-profile"; // (thumbnail, medium, large, full or custom size)
$genreimage = wp_get_attachment_image_src( $attachment_id, $size );
?>
<div class="col-3 py-3">
<div class="mb-2">
<a href="<?php echo get_term_link( $term ); ?>">
<?php if( $attachment_id ) {
echo '<img class="img-fluid border-class shadow-sm" src="'. $genreimage[0] .'" />';
} else {
wp_default_img_placeholder();
}
?>
</a>
</div>
<div class="author-list-name px-1">
<?php echo esc_attr ($term->name); ?>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
<?php endif; ?>
</div>
</div>
</div>
How could I display Genres Parent & Child together another and other? I can display selected parent and child for the custom taxonomy but could not relate with a parent it's just showing one by one not related with the parent but I would like to relate the group Parent-Child :)
-Action
best actor
best music
-Adventure
best music
output
Custom_Movie_Genre_Taxonomy
Thanks a lot for your help!

How to show all categories instead of posts in archive page?

I am trying to show categories in archive page instead of posts which by clicking on category name, show all related posts again in archive page.
<?php
$categories = get_categories();
foreach($categories as $category) {
?>
<!-- Single gallery Item -->
<div class="col-12 col-md-6 single_gallery_item entre wow fadeInUp" data-wow-delay="0.2s">
<!-- Welcome Music Area -->
<div class="poca-music-area style-2 d-flex align-items-center flex-wrap">
<div class="poca-music-content text-center">
<span class="music-published-date mb-2">December 9, 2018</span>
<h2>
<?php echo '' . $category->name . ''; ?>
</h2>
<div class="music-meta-data">
<p>By Admin | Tutorials | 00:02:56</p>
</div>
<div>
<?php
$category_description = category_description();
echo $category_description;
?>
</div>
</div>
</div>
</div>
<?php
}
?>
this is my code for listing categories, but I do not know how to show categories' description and feature images which I added by a plugin by the name of Category featured image.
This quick example:
$categories = get_categories();
foreach($categories as $category) {
$featured_image_id = get_term_meta( $category->term_id, 'featured_image_id', true );
$category_image = wp_get_attachment_image( $featured_image_id, 'thumbnail' );
?>
<p><?php echo $category->name ?></p>
<p><?php echo $category->description ?></p>
<p><?php echo $category_image; ?></p>
<?php
}
Hope it help you.

For some reason wp_reset_postdata isn't working for me.

Hoping someone can help me with this one - I've been able to debug a lot of code over the years with insight from this site.
I'm trying to run two wordpress queries on one page, but for some reason I can't clear out the data from the first query even after using wp_reset_postdata. What am I doing wrong? Should I be trying to query in some other way?
I also tried wp_reset_query, with no success.
This is in a custom theme - do I need something in functions.php to make this work? (I had to that once ages ago for post-thumbnails)
Here's the code:
$args1 = array( 'category' => 'locations' );
$myposts1 = get_posts( $args1 );
foreach ( $myposts1 as $post ) : setup_postdata( $post ); ?>
<div class="post">
<!-- Display the Title as a link to the Post's permalink. -->
<h2 class="post-title">
<?php the_title(); ?>
</h2>
<!-- Display the date (November 16th, 2009 format) and a link to other posts by this posts author. -->
<p class="post-title-meta">
<?php the_time('F jS, Y') ?>
</p>
<!-- Display the Post's Content in a div box. -->
<div class="post-entry">
<?php if ( ( function_exists('has_post_thumbnail') ) && ( has_post_thumbnail() ) ) {
$post_thumbnail_id = get_post_thumbnail_id();
$post_thumbnail_url = wp_get_attachment_url( $post_thumbnail_id );
?>
<div class="post-image">
<img title="image title" alt="thumb image" class="wp-post-image" src="<?php echo $post_thumbnail_url; ?>" style="width:100%; height:auto; margin-bottom:40px;">
</div>
<?php } ?>
<?php the_content( $more_link_text , $strip_teaser ); ?>
</div><!-- post entry-->
</div> <!-- post -->
<?php endforeach;
wp_reset_postdata();
?>
</div> <!-- #col-md-5 -->
<!-- end of Locations -->
<!-- second column-->
<div class="col-md-5">
<h2>Latest </h2>
<?php
$args2 = array( 'category' => 'secondcategory' );
$myposts2 = get_posts( $args2 );
foreach ( $myposts2 as $post ) : setup_postdata( $post ); ?>
<div class="post">
<!-- Display the Title as a link to the Post's permalink. -->
<h2 class="post-title">
<?php the_title(); ?>
</h2>
<!-- Display the date (November 16th, 2009 format) and a link to other posts by this posts author. -->
<p class="post-title-meta">
<?php the_time('F jS, Y') ?>
</p>
<!-- Display the Post's Content in a div box. -->
<div class="post-entry">
<?php if ( ( function_exists('has_post_thumbnail') ) && ( has_post_thumbnail() ) ) {
$post_thumbnail_id = get_post_thumbnail_id();
$post_thumbnail_url = wp_get_attachment_url( $post_thumbnail_id );
?>
<div class="post-image">
<img title="image title" alt="thumb image" class="wp-post-image" src="<?php echo $post_thumbnail_url; ?>" style="width:100%; height:auto; margin-bottom:40px;">
</div>
<?php } ?>
<?php the_content( $more_link_text , $strip_teaser ); ?>
</div><!-- post entry-->
</div> <!-- post -->
<?php endforeach;
wp_reset_postdata();
?>
</div> <!-- #col-md-5 -->
<!-- end of secondcatagory -->

Custom template for category WordPress

Hello I'm new in WordPress custom templates. Can anybody tell me how I can load a different template for a category.
Here is the index.php file
<?php
/**
* The main template file
*
* No comment for now
*
* #package WordPress
* #subpackage site
* #since site
*/
get_header(); ?>
<?php
$strFr='Featured';
$strFinancial='Financial Reports';
$strResearch='Research';
?>
<div id="page-body" role="main">
<div class="container">
<div class="row">
<div class="col-md-12 col-sm-12 col-xs-12 SearchTop">
<form role="search" method="get" id="searchform" class="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<div class="input-group">
<div class="input-group-addon"><i class="fa fa-search"></i></div>
<input type="text" value="<?php echo get_search_query(); ?>" name="s" id="s" class="form-control customsearchfiled" placeholder="Search of anything related to Site" />
</div>
</form>
</div><!-- SearchTop -->
<div class="col-md-12 col-sm-12 col-xs-12">
<div class="col-md-12 col-sm-12 col-xs-12 mb20 no-gutter">
<div class="col-md-12 col-sm-12 col-xs-12 no-gutter">
<?php if (have_posts()) : ?>
<?php /* Start the Loop */ ?>
<?php while (have_posts()) : the_post(); ?>
<?php get_template_part('content', get_post_format()); ?>
<?php endwhile; ?>
<?php else : ?>
<article id="post-0" class="post no-results not-found">
<?php if (current_user_can('edit_posts')) :
// Show a different message to a logged-in user who can add posts.
?>
<header class="entry-header">
<h1 class="entry-title mt20"><?php _e('No posts to display', 'twentytwelve'); ?></h1>
</header>
<!--<div class="entry-content">
<p><?php //printf(__('Ready to publish your first post? Get started here.', 'twentytwelve'), admin_url('post-new.php')); ?></p>
</div> .entry-content -->
<?php else :
// Show the default message to everyone else.
?>
<header class="entry-header">
<h1 class="entry-title"><?php _e('Nothing Found', 'twentytwelve'); ?></h1>
</header>
<div class="entry-content">
<p><?php _e('Apologies, but no results were found. Perhaps searching will help find a related post.', 'twentytwelve'); ?></p>
<?php get_search_form(); ?>
</div><!-- .entry-content -->
<?php endif; // end current_user_can() check ?>
</article><!-- #post-0 -->
<?php endif; // end have_posts() check ?>
</div>
</div>
</div>
</div>
</div>
</div>
<?php get_footer(); ?>
As far I know the index file is calling content php file that shows the post layout but don't know how to do this. Tried a lot of things but only get php error. I don't know php syntax much
<?php
/**
* Custom template with better structure than WP default.
*
* Used for both single and index/archive/search.
*
* #package WordPress
* #subpackage site
* #since Site 1.0
*/
# for one post
if (is_single()) {
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<?php if(function_exists('pf_show_link')){echo pf_show_link();} ?>
<p class="text-center topcategory">
<?php foreach((get_the_category()) as $category) {
echo $category->cat_name . ' ';
}
?>
</p>
<h1 class="entry-title"><?php the_title(); ?></h1>
<p class="postmetadown">By :<span class="bluecolor"><?php the_author(); ?></span> | <span><?php the_date() ?></span></p>
</header>
<div class="entry-content col-md-9 col-sm-9 col-xs-12">
<?php the_content(__('Continue reading <span class="meta-nav">→</span>', 'twentytwelve')); ?>
<?php wp_link_pages(array('before' => '<div class="page-links">' . __('Pages:', 'twentytwelve'), 'after' => '</div>')); ?>
</div>
<div class="col-md-3 col-sm-3 col-xs-12">
<?php get_sidebar(); ?>
</div>
<?php if (comments_open()) : ?>
<!--<div id="disqus_thread"></div>

<script type="text/javascript">
/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
var disqus_shortname = 'site'; // required: replace example with your forum shortname
/* * * DON'T EDIT BELOW THIS LINE * * */
(function () {
var dsq = document.createElement('script');
dsq.type =
'text/javascript';
dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] ||
document.getElementsByTagName('body')
[0]).appendChild(dsq);
})();
</script>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/? ref_noscript">comments powered
by Disqus.</a></noscript>
comments powered by <span class="logo- disqus">Disqus</span>-->
<?php endif; ?>
<footer class="entry-meta col-md-12">
<?php if ($googlePlusAcc = get_the_author_meta('googleplus')) { ?>
<?php hr_single_entry_meta(); ?>
<?php } else { ?>
<?php twentytwelve_entry_meta(); ?>
<?php } ?>
<?php edit_post_link(__('Edit', 'twentytwelve'), '<span class="edit-link">', '</span>'); ?>
<?php if (is_singular() && get_the_author_meta('description') && is_multi_author()) : // If a user has filled out their description and this is a multi-author blog, show a bio on their entries. ?>
<div class="author-info">
<div class="author-avatar">
<?php
/** This filter is documented in author.php */
$author_bio_avatar_size = apply_filters('twentytwelve_author_bio_avatar_size', 68);
echo get_avatar(get_the_author_meta('user_email'), $author_bio_avatar_size);
?>
</div>
<!-- .author-avatar -->
<div class="author-description">
<h2><?php printf(__('About %s', 'twentytwelve'), get_the_author()); ?></h2>
<p><?php the_author_meta('description'); ?></p>
<div class="author-link">
<a href="<?php echo esc_url(get_author_posts_url(get_the_author_meta('ID'))); ?>"
rel="author">
<?php printf(__('View all posts by %s <span class="meta-nav">→</span>', 'twentytwelve'), get_the_author()); ?>
</a>
</div>
</div>
</div>
<?php endif; ?>
</footer>
</article>
<?php
#for listing page
} else {
$category = get_the_category();
if (!empty($category))
$category = $category[0];
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php //if (is_sticky() && is_home() && !is_paged()) : ?>
<!--<div class="featured-post">
<?php //_e('Featured post', 'twentytwelve'); ?>
</div>-->
<?php //endif; ?>
<div class="col-md-4 col-sm-4 col-xs-12 categoryimg no-gutter">
<?php if (has_post_thumbnail()) { ?>
<div class="post-thumbnal-wrapper-multiple">
<?php the_post_thumbnail(); ?>
</div>
<?php } ?>
</div>
<div class="col-md-8 col-sm-8 col-xs-12 categoryContent">
<header class="entry-header">
<h1 class="entry-title">
<?php the_title(); ?>
</h1>
</header>
<?php if (is_search()) : // Only display Excerpts for Search ?>
<div class="entry-summary">
<?php the_excerpt(); ?>
</div>
<?php else : ?>
<div class="entry-content">
<?php the_excerpt_more(); ?>
</div>
<?php endif; ?>
<div class="col-md-12">
<footer class="entry-meta">
<?php twentytwelve_entry_meta(); ?>
<?php edit_post_link(__('Edit', 'twentytwelve'), '<span class="edit-link">', '</span>'); ?>
</footer>
</div>
</div><!-- categoryContent -->
</article>
<div class="clearfix"></div>
<?php
}
If you want a template for the category page, see https://codex.wordpress.org/Category_Templates. If you want a different template for the content, based on the category, you could use (in your content.php)
<?php if (is_category('Category A')) : ?>
<p>This is the text to describe category A</p>
<?php elseif (is_category('Category B')) : ?>
<p>This is the text to describe category B</p>
<?php else : ?>
<p>This is some generic text to describe all other category pages,
I could be left blank</p>
<?php endif; ?>
If there's a lot of difference between categories, I would place this in index.php, and create different templates for every category
Check out this page Template Hierachy. Basically if you want to target a specific category you create a file like so category-{slug}.php {slug} being the category slug you want to target.
I suggest you to read about Wordpress Template Hierarchy in https://developer.wordpress.org/themes/basics/template-hierarchy/
To answer your question, you need to create a category-{id}.php or category-{slug}.php file. in your theme folder (Choose only one)
id = your category ID
slug = your category slug
After creating the file, then you can put your custom code if you want.
Add your code inside archive.php which will list all the posts in the category and add the detail page template inside single.php .
https://codex.wordpress.org/Category_Templates

How to display from category 17 only one post?

I have some code that I was trying to display only one post from the category 17. But I can't it. Anybody have any idea? my code is below. I have tried it but show all post of category 17. I want to show only one post from cat 17.
<?php
if(have_posts()):?>
<?php while(have_posts()):?>
<?php the_post();?>
<?php
$post = $wp_query->post;
if ( in_category( '17' ) ) {
?>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<div class="car_box shadow left">
<h1><?php the_title(); ?></h1>
<!--<img src="images/car2.png" alt="PREMIUM CARS" align="middle"/>-->
<?php
// Post Image.
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
else {
echo '<img src="' . get_bloginfo( 'stylesheet_directory' ) . '/images/thumbnail-default.jpg" />';
}
?>
<p>BMW 7 Series or simillar</p>
<div class="desc left">
<div class="passenger left">
4
</div>
<div class="suitcase left">
3
</div>
<div class="doors left">
5
</div>
<div class="gear left">
Auto
</div>
</div>
View More
</div>
</a>
<?php } ?>
<?php endwhile;?>
<?php endif;?>
Add break; in the loop after you've shown your first found post, that would be before you close the inner if statement.

Resources