Как отобразить запрос WordPress с помощью Ajax

Попытка отображения содержимого с помощью jQuery дает мне некоторые проблемы. Я создал три кнопки, при нажатии они загружают данные из php-файла на моем сервере. Все работает, за исключением случаев, когда я пытаюсь отправить запрос на загрузку сообщений WordPress. Появится следующая фатальная ошибка: вызов функции undefined get_posts (). Я узнал, как это сделать с https://perishablepress.com/slide-fade-content/ .

Для сценариев у меня есть следующее:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <script type="text/javascript" src="http://example.com/slide-fade-content/jquery.colorfade.min.js"></script> <script type="text/javascript"> // slide & fade content @ http://m0n.co/r $(document).ready(function() { $('.more').on('click', function(e) { e.preventDefault(); var href = $(this).attr('href'); if ($('#ajax').is(':visible')) { $('#ajax').css({ display:'block' }).animate({ height:'0' }).empty(); } $('#ajax').css({ display:'block' }).animate({ height:'200px' },function() { $('#ajax').html('<img id="loader" src="loader.gif">'); $('#loader').css({ border:'none', position:'relative', top:'24px', left:'48px', boxShadow:'none' }); $('#ajax').load('slide-fade-content.php ' + href, function() { $('#ajax').hide().fadeIn('slow').colorFade({ 'fadeColor': 'rgb(253,253,175)' }); }); }); }); }); </script> 

Это мои три кнопки:

Здесь отображается содержимое

 <div id="ajax"></div> 

И это содержание

 <div id="load"> <div id="first-item"> <?php $args = array( 'posts_per_page' => 5, 'offset'=> 0, 'category' => '2,3,4', 'orderby' => 'meta_value_num', 'meta_key' => 'views', 'order' => 'DESC', 'suppress_filters' => true, 'date_query' => array( 'after' => date("jS F, Y", strtotime('-24 hours')) ) ); $myposts = get_posts( $args ); foreach ( $myposts as $post ) : setup_postdata( $post ); ?> <div> <li> <ul><?php $categories = get_the_category(); $separator = ' '; $output = ''; if($categories){ foreach($categories as $category) { $output .= '<a href="'.get_category_link( $category->term_id ).'" title="' . esc_attr( sprintf( __( "View all in %s" ), $category->name ) ) . '">'.$category->cat_name.'</a>'.$separator; } echo trim($output, $separator) ; } ?> </ul> <a href="<?php the_permalink(); ?>"><?php echo get_the_post_thumbnail( $post_id, array(300, 160), $attr ); ?> <a href="<?php the_permalink(); ?>"><?php the_title(); ?> </li> </div> <?php endforeach; wp_reset_postdata();?> </div> <div id="second-item"> Test2 </div> <div id="third-item"> Test3 </div> </div> 

Я думаю, что мне нужно определить функцию, но после многих часов проб и ошибок я теряюсь. Я не могу понять, как это сделать или где его определять.

Related of "Как отобразить запрос WordPress с помощью Ajax"