
Load post inside div with AJAX in Wordpress
I’m building a website with Wordpress. To resume, I would like to load posts (from my custom post type ‘project’) dynamically with AJAX inside the div .js-panel-content , by clicking a button with the same ID than the post loaded. The issue is, when clicking the button it loads all the posts of my custom post type. I tried to print the ID of the post via AJAX just to see if the ID was loaded correctly. I did it by replacing the line panelContent.innerHTML = data; with panelContent.innerHTML = post_id; The result is, the ID is undefined. The HTML/PHP <?php $args = array( 'post_type' => 'project', 'posts_per_page' => -1, ); $query = new WP_Query( $args ); while( $query->have_posts() ) : $query->the_post(); echo '<button id="post-'. get_the_ID() .'" class="js-btn-load-post">'. get_the_title() .'</button>'; endwhile; ?> <div class="c-panel js-panel" data-ajax-url='<?php echo esc_url( home_url( '/' ) ); ?>wp-admin/admin-ajax.php'> <div class="c-panel__content js-panel-content"> </div> </div
Continue reading on Dev.to JavaScript
Opens in a new tab




