Support » Plugin: Jetpack by WordPress.com » Infinite Scroll – page/2/ repeating same content as page/1/

  • Resolved kareemsayeed

    (@kareemsayeed)



    Hi Everyone, I’ve been using this awesome plugin (infinite scroll feature) for well over a year. I had to initally add this to my functions.php to get it to work, but after that everything was great..

    function mytheme_infinite_scroll_init() {
    add_theme_support( 'infinite-scroll', array(
    'container' => 'content',
    'render' => 'mytheme_infinite_scroll_render',
    'footer' => 'footer',
      'posts_per_page' => 60,
    ) );
    }
    add_action( 'init', 'mytheme_infinite_scroll_init' );
    
    function mytheme_infinite_scroll_render() {
    get_template_part('loop-actions');
    			get_template_part('loop-content');
    }

    Today I decided to tweak my loop-content.php to group posts of the same date with another.

    original loop-conent.php:

    <?php
    		global $loop_view;
    		$ajaxload = get_option('dp_archive_ajaxload');
    	?>
    
    	<div class="loop-content switchable-view <?php echo $loop_view; ?>" data-view="<?php echo $loop_view; ?>" data-ajaxload=<?php echo $ajaxload; ?>>
    		<div class="nag cf">
    			<?php while (have_posts()) : the_post();
    				get_template_part('item-video');
    			endwhile; ?>
    		</div>
    	</div><!-- end .loop-content -->

    Current loop-content.php:

    <?php
    		global $loop_view;
    		$ajaxload = get_option('dp_archive_ajaxload');
    	?>
    
    	<div class="loop-content switchable-view <?php echo $loop_view; ?>" data-view="<?php echo $loop_view; ?>" data-ajaxload=<?php echo $ajaxload; ?>>
    
    		<div class="nag cf">
    
    <?php
      global $wpdb;
      $splitformat = '%B %d, %Y';
      $sql = "SELECT ID, post_title, post_date
    			FROM $wpdb->posts
    			WHERE post_type = 'post' AND post_status = 'publish' AND post_date > '" . date('Y-m-d', strtotime('-3 days')) . "'
    			ORDER BY post_date DESC
    			";
      $posts = $wpdb->get_results($sql);
      $archives = array();
      foreach($posts as $post)
    	{
    		$key = strftime($splitformat, strtotime($post->post_date));
    		$archives[$key][] = $post;
    	}
     $output = $pre_HTML;
      	foreach($archives as $heading => $posts)
    	{
    	echo '<h3><hr>' . $headingtag . '' . htmlentities($heading) . '</h3>' . $headingtag . '</h3><hr>', "\n";
    		echo '', "\n";
    		foreach($posts as $post)
    		{
    get_template_part('item-video');
    
    		}
    		echo '</ul>', "\n\n";
    	}
    ?>
    		</div>
    	</div><!-- end .loop-content -->

    Unfortunatley when I scroll down to page/2/ it shows the same content as my homepage. I’ve been working on this all day, if anyone can figure it out I will be so very grateful.

    https://wordpress.org/plugins/jetpack/

Viewing 7 replies - 1 through 7 (of 7 total)
  • Howdy!

    After checking your code, I notice in your wpdb query that no parameters are being specified that would consider pagination. I see no page or paged parameters being specified into the custom query.

    Additionally, you are using wpdb. This is not a good approach to use, specially due to its advanced nature. It’s very low level and WP_Query would be used instead. It has several date related query parameters that can be used for date queries:

    https://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters

    I would suggest trying to use WP_Query for the date query, and make sure the paged parameter is specified on the query in order for pagination to be considered.

    You can get the current page value by doing something like this:

    global $paged;
    $paged = (0 == $paged) ? 1 : (int) $paged;

    Hope it helps,
    Cheers!

    Thank you mendezcode, I’m very unexperienced with coding. so all I did was update global $wpdb; with `global $paged;
    $paged = (0 == $paged) ? 1 : (int) $paged;`

    And It still has the same issue (I’m sure you meant something else). Is there a way I can commission you to help me with this project?

    Hi! If you tell me *exactly* what you’re trying to achieve I may be able to help you out with the code, but right now I don’t seem to understand this:

    Today I decided to tweak my loop-content.php to group posts of the same date with another.

    What do you mean by grouping posts of the same date with another?

    The more information you can provide, the easier it would be for me to come up with a query string that would work.

    Regards

    Ah Ok! (Thank you for your help so far)

    If you go to my website battledomination.com you will see three days worth of posts (Jan 11-Jan 8), but once you scroll down to page/2/ it loads the same three days again (Jan 11-Jan 8).

    Instead I want page/2/ to load the next three days (Jan 7-Jan 4)
    And so on.

    Also, if you hover over the title of each post you’ll see that the permalink isn’t working anymore. It just directs the user to the homepage.. The video thumbnails permalinks still work though.

    Any thoughts?

    Hello Kareem,

    Apologies for the delay answering…

    I’ve been trying to figure out a way to do this, but have hit a wall every time. The query you are after would not be that easy to implement, since there are many factors involved, which would render the pagination inconsistent:

    1. The number of posts returned for a given date may vary. This means pagination is not consistent, and we need consistency in the query in order for IS to work properly.

    2. Each query of posts would have its own pagination (per date query), independent of the main pagination desired to work with IS.

    That being said, the desired approach you are after would require a very advanced query to pull through.

    I think hiring a freelancer would be the best way to go in this case, but be aware that the query you’re trying to achieve may not provide consistent results.

    Regards

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Infinite Scroll – page/2/ repeating same content as page/1/’ is closed to new replies.