ajax - WordPress custom post type post item data - showfetch only on click with Javascript dynamically?

admin2025-01-07  4

I have a question. Hopefully, I will be able to explain what I am trying to do here.

I have created a custom post type "Feeds" and published 10 post types inside that CP.

Now, usually, I would just loop through the CP and display them on a page on page load and display them in the DOM.

PHP will normally output everything when you load the page.

What I am trying to do, I have created a page displaying all the custom post type posts.

But when I click on one post I would like to open a modal which I have created in HTML, CSS, and Javascript which works statically.

I need only ONE modal HTML output to display the specific opened post type title, content and featured image on request/click only using JS.

I don't want to load 10 HTML modal DIv's for each post type instead load them only on request when needed.

Does anyone know, or can give me a simple example of how I can accomplish this using Javascript or Ajax?

Thanks in advance.

I have a question. Hopefully, I will be able to explain what I am trying to do here.

I have created a custom post type "Feeds" and published 10 post types inside that CP.

Now, usually, I would just loop through the CP and display them on a page on page load and display them in the DOM.

PHP will normally output everything when you load the page.

What I am trying to do, I have created a page displaying all the custom post type posts.

But when I click on one post I would like to open a modal which I have created in HTML, CSS, and Javascript which works statically.

I need only ONE modal HTML output to display the specific opened post type title, content and featured image on request/click only using JS.

I don't want to load 10 HTML modal DIv's for each post type instead load them only on request when needed.

Does anyone know, or can give me a simple example of how I can accomplish this using Javascript or Ajax?

Thanks in advance.

Share Improve this question asked Aug 14, 2019 at 16:43 GalanthusGalanthus 1112 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I think you should be able to do this:

HTML:

<a class="view_post_details" href="#" post_id="<?php the_ID(); ?>">View Post Details</a>

JS:

$(".view_post_detals").click(function () {

   var id_post = $(this).attr('post_id');

    $.ajax({
       type: 'POST',
       url: '<?php echo admin_url('admin-ajax.php'); ?>',
       data: {
          'post_id': id_post,
          'action': 'f711_get_post_content' //this is the name of the AJAX method called in WordPress
       }, success: function (result) {

           alert(result);
       },
       error: function () {
           alert("error");
       }
   });
});
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736255884a317.html

最新回复(0)