weixin_33739646 2015-01-12 06:28 采纳率: 0%
浏览 34

我如何在Wordpress中使用Ajax

I am new to ajax. I want to place a combobox on page. Combobox contain option by popularity, by price and by date.

I want to change the content of div with ajax without reloading the page.

e.g i am displaying a product of watches. when user change the value at combo box, the value/images displayed at page will be change as accordingly.

1) i want to know how to use ajax with word press. 2)code to achieve this.

I am using this code:

            <div class="prodimg">


<ul class="rcollpro">

    <?php
        $ordr ='date';
        $args = array( 'post_type' => 'product',  'product_cat' => 'Rings', 'stock' => 1, 'posts_per_page' => 12, 'orderby' =>'$ordr','order' => 'DESC' );
        $loop = new WP_Query( $args );
        while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>

                <li class="rcollproli">    

                    <a href="<?php the_permalink(); ?>"><?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="65px" height="115px" />'; ?></a>

                        <h3><?php the_title(); ?></h3>

                           <span class="price"><?php echo $product->get_price_html(); ?></span><br />



                    <?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>
                </li><!-- /span3 -->
    <?php endwhile; ?>
    <?php wp_reset_query(); ?>

</ul><!-- /row-fluid -->

            </div>
  • 写回答

2条回答 默认 最新

  • 胖鸭 2015-01-12 07:42
    关注

    You dont actually need to implement Ajax, but you can do it with simple JS. I can share a simple code with you for your reference.

    <script type="text/javascript">
     function hourChange(selectObj) {
       var selectIndex=selectObj.selectedIndex;
       var selectValue=selectObj.options[selectIndex].text;
       var output=document.getElementById("output");
       //alert(output.innerText);
       output.innerHTML=selectValue;
     }
    
    </script>
    
    <select id="hour" onchange="hourChange(this);">
    
      <option>1</option>
      <option>2</option>
    
    </select>
    
    <p/>
    
    you selected: <span id="output">1</span>
    

    Try this in simple HTML file. And it's easy to implement it in WP too.

    评论

报告相同问题?

悬赏问题

  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3