Wordpress load more posts on front page (page.php) - loads empty content

167
July 17, 2017, at 8:36 PM

I've made load more button on the bottom on my page.php. Unfortunately, it doesn't load anything despite of showing success in ajax.

Code as follows:

myloadmore.js

jQuery(function($){

$('.load-articles').on('click', function() {
    var self = $(this),
            page = self.data('page'),
            newPage = page + 1,
            ajaxurl = self.data('url'),
            post_types = self.data('types'),
            post_template = self.data('tmpl');
    $.ajax({
        url  : ajaxurl,
        type : 'post',
        data : {
            page   : page,
            types  : post_types,
            action : 'more_articles_ajax',
            tmpl   : post_template
        },
        beforeSend : function ( xhr ) {
            $('.load-articles').text('Ładuję...'); // change the button text, you can also add a preloader image
        },
        error : function( response ) {
            console.log( response );
        },
        success : function( response ) {
            console.log(response);
            if (response) {
                self.data('page', newPage);
                $('.loadmore-container').hide().append(response).fadeIn(500);
                $('.load-articles').text('zobacz więcej');
            } else {
                $('.load-articles').remove();
            }
        }
    });
  });
});

my function in functions.php

function more_articles_ajax() {
    $paged = $_POST['page'] + 1;
    $types = $_POST['types'];
    $tmpl = $_POST['tmpl'];
    $query = new WP_Query( array(
        'post_type'   => array($types),
        'posts_per_page' => 6,
        'paged' => $paged,
        'order' => 'ASC',
    ) );
    if( $query->have_posts() ):
        while( $query->have_posts() ): $query->the_post();
            get_template_part( 'template-parts/content', $tmpl);
        endwhile;
    endif;
    wp_reset_postdata();
    die();
}
add_action('wp_ajax_nopriv_more_articles_ajax', 'more_articles_ajax');
add_action('wp_ajax_more_articles_ajax', 'more_articles_ajax');

and page content:

<?php
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$args = array(
  'numberposts' => 999,
  'post_type'   => array('news'),
  'order' => 'DESC',
  'posts_per_page' => 2,
  'paged' => get_query_var('page'),
  'page' => $paged
);
$latest_services = new WP_Query ( $args );
$i = 0;
?>
 <?php if ($latest_services->have_posts()): while ($latest_services->have_posts()) : $latest_services->the_post();?>
//some posts
<?php endwhile; ?>
<div class="loadmore-container"></div>
<div class="loadmore column load-articles" data-page="1" data-types="news" data-tmpl="article-item" data-url="<?php echo admin_url('admin-ajax.php'); ?>">see more</div>
<?php else: ?>
<?php endif; wp_reset_postdata(); ?>

Edit: Ok, I just have to add, that when entering sth as www.home-url/page/2/ I can see that my content is paged. However, when I add my pagination template, it doesn't show anything (but it works good on other pages).

READ ALSO
PHP XML error - extra content at the end of the document

PHP XML error - extra content at the end of the document

I have the PHP code below, intended to create an XML document from 3 columns and multiple rows in an MySQL databaseI am getting the following error message:

198
Sonata Type Collection, add and edit form in a new window (popup)

Sonata Type Collection, add and edit form in a new window (popup)

I am doing a project with sonata admin and I have a two entities, with a relation OneToMany and an ArrayCollection to manage it

170
Skip foreach if same value as database

Skip foreach if same value as database

First of all sorry for my bad englishI would like to ask you how to skip foreach loop from xml if a value is in the database

164