@harts_1493

Как вывести пост в модальном окне с помощью ajex?

Здравствуйте, нужна ваша помощь.
Есть код, который переделал под bootstrap, но не выводить сам пост. Также не выводить ошибку.
<a data-toggle="modal" href="#post-<?php the_ID(); ?>" title="<?php the_title(); ?>">пост 1</a>
<div class="ajex-modal modal fade" id="post-<?php the_ID(); ?>" post="<?php the_ID(); ?>" tabindex="-1" role="dialog" aria-labelledby="postLabel"></div>

ajex.js
(function($) {
	$(document).ready(function(){
		$('.ajex-modal').on('shown.bs.modal', function (e) {
			var post_id = $(this).attr( 'post' );
			$.ajax({
				cache: false,
				timeout: 8000,
				url: php_array.admin_ajax,
				type: "POST",
				data: ({ action:'theme_post_example', id:post_id }),
				beforeSend: function() {$('.ajex-modal[post="'+post_id+'"]').html( '<i class="fa fa-spinner fa-pulse fa-3x fa-fw"></i><span class="sr-only">Загрузка...</span>' );},
				success: function( data, textStatus, jqXHR ){
					var $ajax_response = $( data );
					$('.ajex-modal[post="'+post_id+'"]').html( $ajax_response );
				},
				error: function( jqXHR, textStatus, errorThrown ){
					console.log( 'Произошла следующая ошибка: ' + textStatus, errorThrown );
				},
				complete: function( jqXHR, textStatus ){}
			});
		});
		$('.ajex-modal').on('hidden.bs.modal', function (e) {
			$('.ajex-modal').html('');
		});
	});
})(jQuery);

functions.php
add_action( 'wp_enqueue_scripts', 'theme_register_scripts', 1 );
	function theme_register_scripts() {
	  wp_register_script( 'ajex-post', esc_url( trailingslashit( get_template_directory_uri() ) . '/js/ajex.js' ), array( 'jquery' ), '1.0', true );
	  $php_array = array( 'admin_ajax' => admin_url( 'admin-ajax.php' ) );
	  wp_localize_script( 'ajex-post', 'php_array', $php_array );
	}
	add_action( 'wp_enqueue_scripts', 'theme_enqueue_scripts' );

	function theme_enqueue_scripts() {wp_enqueue_script( 'ajex-post' );}	
	add_action( 'wp_ajax_theme_post_example', 'ajex_post' );
	add_action( 'wp_ajax_nopriv_theme_post_example', 'ajex_post' );

	function ajex_post() {
		$args = array( 'p' => $_POST['id']);
		$theme_post_query = new WP_Query( $args );?>	
		while( $theme_post_query->have_posts() ) : 
			$theme_post_query->the_post();?>		
				<div class="modal-dialog modal-lg" role="document">
					<div class="modal-content">
						<div class="modal-body">
							<h1 class="modal-title"><?php the_title(); ?></h1>
							<?php the_content(); ?>	
						</div>
					</div>
				</div>
			<?php 
		endwhile; 
		exit;
	}
  • Вопрос задан
  • 242 просмотра
Решения вопроса 1
@harts_1493 Автор вопроса
Проблему нашел.
Надо было использовать событие show.bs.modal вместо shown.bs.modal.
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы