Wordpress Ajax for newbies

admin

Administrator
Staff member
I am having issues particularly using Ajax post within an Ajax Post. Please enlighten me if this is possible.

My scenario goes like this.

I am using Wordpress and Bootstrap for my theme. I have a post type for "products" which I want to load using Ajax and have been successfully implemented them individually (for testing purposes) using <a href="https://stackoverflow.com/questions/15402239/how-to-load-wordpress-post-with-ajax-onclick">Wordpress post with Ajax on click</a> as well as using Bootstrap modal to load another Ajax post using <a href="http://wpquestions.com/question/showChrono/id/7922" rel="nofollow noreferrer">Modal via Ajax Post</a>.

I am not a hardcore PHP nor jQuery programmer and I want to know if this 2 can be combined in one code which would be like:

On first click will show the first Ajax post, and on the second click inside the first Ajax post will load Bootstrap modal via Ajax as well.

Here are the format of my initial codes ---

jQuery:

Code:
// AJAX POST / stackoverflow.com/questions/15402239/how-to-load-wordpress-post-with-ajax-onclick
// Calls archieve-products.php
$.ajaxSetup({cache:false});
$("a.ajax").click(function(){
    var post_url = $(this).attr("href");
    var post_id = $(this).attr("rel");
        $("#brand-default").hide();
        $("#brand-listing").show().html('&lt;div class="loading"&gt;&lt;/div&gt;');
        $("#brand-listing").load(post_url);
    return false;
});

// MODAL VIA AJAX POST / wpquestions.com/question/showChrono/id/7922
// Opens modal.php
$('[data-toggle="modal"]').click(function(e) {
    e.preventDefault();
    var href = $(this).attr('href');
        if (href.indexOf('#') == 0) {
            $(href).modal('open');
        } else {
            $.get(href, function(data) {
            $('&lt;div id="brands" class="modal hide fade"&gt;' + data + '&lt;/div&gt;').modal();
        });
    }
});

HREF from custom page template:

Code:
&lt;a href="&lt;?php echo $cpt_onomy-&gt;get_term_link( $term, $term-&gt;taxonomy ); ?&gt;" rel="&lt;?php the_ID(); ?&gt;" class="ajax"&gt;

HREF from archive page template:

Code:
&lt;a href="&lt;?php bloginfo('template_url');?&gt;/modal.php?ID=&lt;?php the_ID(); ?&gt;" data-toggle="modal"&gt;

I hope the codes I have provided is enough to help me. <strong>Many thanks in advance :)</strong>