Siros Fakhri
Siros Fakhri

Reputation: 2323

my Load More Posts Ajax Button in Wordpress is not working

I need to Show More Post on Click button

when I click on the button at inspect -> network everything is okay or at least i guess is okay

enter image description here

enter image description here

I just Don't Know Why my post not show on page

here My Code's

Main-Content:

  <div id="all-posts" class="posts-container">
        <?php get_template_part('loops/loop','all'); ?>
        <div class="load-more-content">
            <a data-page="2" href="" class="load-more"> More >> </a>
        </div>
    </div>

Ajax :

//For More Content
add_action('wp_ajax_load_more_content', 'load_more_content');
add_action('wp_ajax_nopriv_load_more_content', 'load_more_content');
function load_more_content() {
 $page          = intval( $_POST['page'] );
 $post_per_page = 1;
 $offset        = ( $page - 1 ) * $post_per_page;
 $output_html   = '';

 $load_more_agrs = array(
    'post_type'      => array('post','download'),
    'offset'         => $offset,
    'posts_per_page' => $post_per_page
);

$load_more_query = new WP_Query( $load_more_agrs );

if ( $load_more_query->have_posts() ):
    while ( $load_more_query->have_posts() ):$load_more_query->the_post();
        $output_html .= ' <a class="post-link" href="' . the_permalink() . '">';
        $output_html .= '<div class="post wow fadeInUp">';
        $output_html .= '<div class="post-inner">';
        $output_html .= '<div class="post-thumb">';
        get_the_post_thumbnail( $load_more_query->post->ID, 'main-thumbnail' );
        $output_html .= '</div>';
        $output_html .= '<span class="post-title">' . get_the_title( $load_more_query->post->ID ) . '</span>';
        $output_html .= '</div>';
        $output_html .= '<div class="post-meta" >';
        $output_html .= '<span ><i class="fa fa-clock-o" ></i>' . get_the_date( 'y,m,d', $load_more_query->post->ID ) . '</span>';
        $output_html .= '<span><i class="fa fa-user"></i>' . get_the_author() . '</span>';
        $output_html .= '<span><i class="fa fa-eye"></i>' . get_post_view( get_the_ID() ) . '</span>';
        $output_html .= '<span><i class="fa fa-thumbs-o-up"></i></span>';
        $output_html .= '</div>';
        $output_html .= '</div>';
        $output_html .= '</a>';
    endwhile;
endif;
$count = $load_more_query->found_posts;
wp_reset_postdata();
$result            = array();
$result['count']   = $count;
$result['content'] = $output_html;
die( json_encode( $result ) );

}

I think my problem is Ajax Code's when I add dataType:'json', I got Error. I mean Error Function execute so I have to add contentType: 'application/json', but then my Response is Only 0.

Ajax Code :

 $(document).on('click','.load-more', function (event) {
    event.preventDefault();
    var $this = $(this);
    $this.text('Loading...');
    var $page = parseInt($this.data('page'));
    $.ajax({
        url: data.ajax_url,
        type:'post',
        /*contentType: 'application/json',*/
        //dataType:'json',
        data: {
            action: 'load_more_content',
            page: $page
        },
        success: function (response) {
            if (parseInt(response.count) > 0) {
                $this.parent().before(response.content);
                $this.data('page', parseInt($page + 1));
            }
            $this.text('Done!');
        },
        error: function (err) {
            alert('Error is : '+ err.statusText);
        }
    });
});

});

Upvotes: 0

Views: 1017

Answers (1)

user8158124
user8158124

Reputation:

The response shown in the network tab is not correct JSON.

The problem is caused by the use of the_permalink() which echos its result. You should use get_permalink() which returns a string.

Also, although not related to this particular problem but the result of get_the_post_thumbnail() is not being appended to $output_html.

Upvotes: 1

Related Questions