Post content in bootstrap modal

Question

I tried to open post content in a bootstrap modal window, but something is not working correctly. I don’t know what the error is and I would like some help to understand.

Post display:

<?php $args = array(
'p' => $postID, // ID поста
'post_type' => 'any'
);
$recent = new WP_Query($args);
while ( $recent->have_posts() ) : $recent->the_post();?>

<a data-ajax-param="<?php the_ID(); ?>" data-toggle="modal" data-target="#postModal"><?php the_title() ?></a>

<?php endwhile; ?>

Bootstrap modal:

<div class="modal fade" id="postModal">
    <div class="modal-dialog modal-lg">
        <div class="modal-content ajax">

        </div>
    </div>
</div>

Enabled AJAX support and declared function in functions.php:

add_action('wp_ajax_my_action', 'data_fetch');
add_action('wp_ajax_nopriv_my_action', 'data_fetch');

function data_fetch($postID){
    $postID=intval( $_POST['param'] );
    
    $args = array(
    'p' => $postID, // ID of a page, post, or custom type
    'post_type' => 'movies'
    );

    $recent = new WP_Query($args);

    while ( $recent->have_posts() ) : $recent->the_post();?>

        <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <?php the_title( '<h2 class="modal-title text-center">', '</h2>' ); ?>
        </div>

        <div class="row">

            <div class="col-lg-9">
                <?php the_content(); ?>
            </div>
        </div>

    <?php endwhile; ?>
<?php
    die();
}

Jquery scripts for the function to work, I added in main.js:

function fetch(e){var param = $(e.target).attr('data-ajax-param');
  $.post('wp-admin/admin-ajax.php', {'action':'my_action', 'param':param}, function(response){
   $('.modal-content.ajax').html(response);
});
}

//-------------

$( '[data-ajax-param]' ).click(function (e) {
 fetch(e);
});

The posts is displayed, but when I try to open them, nothing happens.
I would like to understand what I did not enter correctly?

0
Genetic Marshall 2 months 2022-12-11T14:13:40-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse