A
A
alexander_chn2018-04-17 17:44:17
WordPress
alexander_chn, 2018-04-17 17:44:17

How to fix duplicate posts in wordpress?

Hello! The bottom line is that there are repetitions on the output page of the records. as I understand it, they are because of the Show more button. Here is the code that outputs the entries:

<div class="b-blog-flow_flow" id="blog-posts">
            <?
            while ($posts->have_posts()) {
                $posts->the_post(); ?>
                <div class="b-blog-flow_item">
                    <div class="b-blog-flow_item-image-wrap">
                        <?php echo get_the_post_thumbnail(null, 'blog-thumb'); ?>
                    </div>
                    <div class="b-blog-flow_item-text">
                        <div class="b-blog-flow_item-date"><?php the_date('d.m.Y'); //echo get_the_date('h:i  |  G F Y');?></div>
                        <div class="b-blog-flow_item-content">
                            <div class="b-blog-flow_item-title"><?echo the_title();?></div>
                            <div class="b-blog-flow_item-short-text"><?echo the_excerpt();?></div>
                        </div>
                        <a class="e-more-link b-blog-flow_item-readmore" href="<? echo get_permalink();?>">Читать пост</a>
                    </div>
                </div>
                <?
            }
            ?>
        </div>
        <?php if ($total > $cnt) : ?>
        <div class="b-works-flow_more-button-wrap">
            <div class="e--button v--height-1 v--size-1 v--type-1 js-show-more">
                <div class="e--button__link">Показать больше</div>
            </div>
        </div>
        <?php endif; ?>

Here is the script that runs when you click on the Show more button:
<script>
    var cat = <?php echo $category ?>;
    var total = <?php echo $total ?>;
    var page = 2;
    $(function(){
      $('.js-show-more').click(function(){
          var params = {
              'page': page,
              'cat': cat,
          };
          var data  = jQuery.param(params);
          var action = 'getblogposts';
          sendForm(action, data, function (answer) {
              page++;
              $('#blog-posts').append(answer);
              if (($('#blog-posts .b-blog-flow_item').length)>=total) $('.b-works-flow_more-button-wrap').remove();
          });
      });
    })
</script>

Here is the php code for displaying the entry after clicking Show more:
function show_more_posts(){
    $nonce = $_POST['nonce'];
    if (!wp_verify_nonce($nonce,'email-nonce')) die('bad request');
    $category = intval($_POST['cat']);
    $page = intval($_POST['page']);
    $query = [
        'cat' => $category,
        'posts_per_archive_page' => 5,
        'paged' => $page
    ];

    $posts = new WP_query();
    $posts->query($query);

    while ($posts->have_posts()) {
        $posts->the_post();
        ?>
        <div class="b-blog-flow_item">
            <div class="b-blog-flow_item-image-wrap">
                <?php echo get_the_post_thumbnail(null, 'blog-thumb'); ?>
            </div>
            <div class="b-blog-flow_item-text">
                <div class="b-blog-flow_item-date"><?echo get_the_date('G F Y');?></div>
                <div class="b-blog-flow_item-content">
                    <div class="b-blog-flow_item-title"><?echo the_title();?></div>
                    <div class="b-blog-flow_item-short-text"><?echo the_excerpt();?></div>
                </div>
                <a class="e-more-link b-blog-flow_item-readmore" href="<? echo get_permalink();?>">Читать пост</a>
            </div>
        </div>
    <?php }
 die();
}

I can not figure out how to correct the code so that the entries do not repeat?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question