Jimmy Neutron2017-01-23 13:31:43
Jimmy Neutron, 2017-01-23 13:31:43

Ajax article loading. What's wrong?

Guys, I took a ready-made script that was written a long time ago, installed it and everything seems to work, but not as it should. What could be wrong here?
It is necessary that he displays from one category, but I can not figure out how to do this.
And is it possible to make it so that there is 1 button and works for 2 functions (since the template has 2 columns and each has a separate category) and loads both

. I redid it for myself


function true_loadmore_scripts() {
 	wp_enqueue_script( 'true_loadmore', get_stylesheet_directory_uri() . '/loadmore.js', array('jquery') );
add_action( 'wp_enqueue_scripts', 'true_loadmore_scripts' );

function true_load_posts(){
  // News
  $args = unserialize(stripslashes($_POST['query']));
  $args['paged'] = $_POST['page'] + 1;
  $args['post_status'] = 'publish';
  $q = new WP_Query($args);
   if (have_posts()) : while (have_posts()) : the_post(); ?>
      <div class="container">
        <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
        <?php the_excerpt(); ?></p></div>
add_action('wp_ajax_loadmore', 'true_load_posts');
add_action('wp_ajax_nopriv_loadmore', 'true_load_posts');

News page

<div class="news__right">
      <?php query_posts('category__in=1&showposts=4&order=DESC'); ?>
      <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
        <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
        <?php the_excerpt(); ?>
      <?php endwhile;?>
      <?php if (  $wp_query->max_num_pages > 1 ) : ?>
      var ajaxurl = '<?php echo site_url() ?>/wp-admin/admin-ajax.php';
      var true_posts = '<?php echo serialize($wp_query->query_vars); ?>';
      var current_page = <?php echo (get_query_var('paged')) ? get_query_var('paged') : 1; ?>;
      var max_pages = '<?php echo $wp_query->max_num_pages; ?>';
  <?php endif; ?>
  	<? else: ?>
        <?php _e('К сожалению, по Вашему запросу ничего не найдено.'); ?>
    <?php endif; ?>


    $(this).text('Загружаю...'); // изменяем текст кнопки, вы также можете добавить прелоадер
    var data = {
      'action': 'loadmore',
      'query': true_posts,
      'page' : current_page
      url:ajaxurl, // обработчик
      data:data, // данные
      type:'POST', // тип запроса
        if( data ) { 
          $('#true_loadmore').text('Загрузить ещё').before(data); // вставляем новые посты
          current_page++; // увеличиваем номер страницы на единицу
          if (current_page == max_pages) $("#true_loadmore").remove(); // если последняя страница, удаляем кнопку
        } else {
          $('#true_loadmore').remove(); // если мы дошли до последней страницы постов, скроем кнопку

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Jimmy Neutron, 2017-01-23

Okay, I figured out the loading of a certain category. It was necessary to put query_post instead of have_post
There is one task left in terms of 1 button for 2 functions

Egor Maltsev, 2017-01-23

You can add one more parameter to the true_load_posts() function $args['taxonomy_name'] = 'term_name'; Or in your case the category and the name of the category.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question