• Resolved hifumi

    (@hifumi)


    I’m trying to combine Masonry with Infinite-Scroll, both from Metafizzy website, so that when I scroll downwards, it’ll automatically grab previous posts.

    The Infinite-Scroll works but it only calls one post from blog/page/2 whereas it has three posts, but each time I scroll downwards a new post spawns on-top overlapping each other and the appended item does not align with the rest of the masonry grid either.

    When I scroll back up and down again it begins to call blog/page/3, however I didn’t make many posts so the 3rd page does not exist.

    Functions

    function enqueue_masonry() {
            wp_enqueue_script( 'masonry', get_template_directory_uri() . '/js/masonry.js', array( 'jquery' ), '', true );
            wp_enqueue_script( 'jquery-masonry', true );
            wp_enqueue_script('masonry', true);
            wp_enqueue_script( 'infinitescroll', get_template_directory_uri() . '/js/infinite-scroll.pkgd.min.js', array( 'jquery' ), '', true );
    }
    add_action( 'wp_enqueue_scripts', 'enqueue_masonry');

    Section Index

    <div class="js-masonry">
      <?php if( have_posts() ): while( have_posts() ): the_post();?>
        <div class="item-masonry overlay">
          <a href="<?php the_permalink();?>">
          <div class="posts">
            <?php foreach((get_the_category()) as $category) {echo $category->cat_name . ' ';}?>
            <h1><?php the_title();?></h1>
            ————
            <h4><?php the_author();?></h4>
          </div>
          <img src="<?php the_post_thumbnail_url('');?>"/>
          </a>
        </div>
      <?php endwhile; else: endif;?>
    </div>
    
    <p class="pagination">
        <a class="pagination__next"><?php next_posts_link( 'Older Entries »', 0 ); ?></a>
    </p>

    Index

    <div class="masonry">
        <?php get_template_part('includes/section','index');?>
    </div>
    
    <script>
    var msnry = document.querySelector('.masonry');
            var msnry = new Masonry( msnry, {
              itemSelector: '.item-masonry',
              columnWidth: '.size-masonry'
            });
    var infScroll = document.querySelector('.masonry');
            var infScroll = new InfiniteScroll( '.masonry', {
              path: 'page/{{#}}',
              append: '.item-masonry',
              debug: true,
              hideNav: '.pagination',
              outlayer: msnry,
            });
    </script>

    I’ve tried changing the path to pagination__next but it doesn’t do anything.

    • This topic was modified 3 years, 8 months ago by hifumi. Reason: text formatting
    • This topic was modified 3 years, 8 months ago by hifumi. Reason: added alternative attempts
Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter hifumi

    (@hifumi)

    I found out it does load all the available posts in blog/page/2 but are overlapping on each other based on inspecting the element tab.

    I’ve also searched that I’m missing the imagesLoaded code but I don’t know how to combine it with my existing vanilla script. When I try to add:

    var imgLoad = imagesLoaded (msnry);
     function onAlways ( instance ) {
     console.log('all images are loaded');
     }

    It does not do anything.

    • This reply was modified 3 years, 8 months ago by hifumi.
    • This reply was modified 3 years, 8 months ago by hifumi.
    Moderator bcworkz

    (@bcworkz)

    Be sure the lazy load is adding elements in a manner compatible with masonry. I think you may need to reinitialize masonry after lazy loading. Otherwise masonry thinks it’s done and is unaware that elements have been added. It needs to reflow the entire container.

    .imagesLoaded() needs an object to operate upon, for example

    $('.masonry').imagesLoaded( function() {
      // do something after images have loaded in .masonry
    });
    Thread Starter hifumi

    (@hifumi)

    So I tried both options:

    var imgLoad = imagesLoaded (masonry);
     function onAlways ( instance ) {
     console.log('all images are loaded');
     }

    Which results in the error:
    (index):214 Uncaught ReferenceError: masonry is not defined at (index):214
    When I change it to this:

    var imgLoad = imagesLoaded (.masonry);
     function onAlways ( instance ) {
     console.log('all images are loaded');
     }

    This gives me:
    Uncaught SyntaxError: Unexpected token '.'
    So Lazyload is in the latest WordPress 5.5 update, this is automatically enabled is that correct? I’ve updated the code to reinitialize the masonry:

    <script>
    var msnry = document.querySelector('.masonry');
            var msnry = new Masonry( msnry, {
              itemSelector: '.item-masonry',
              columnWidth: '.size-masonry',
              initLayout: false
            });
    msnry.on( 'layoutComplete', function( items ) {
      console.log( items.length );
    });
    msnry.layout();
    
    var infScroll = document.querySelector('.masonry');
            var infScroll = new InfiniteScroll( '.masonry', {
              path: 'page/{{#}}',
              append: '.item-masonry',
              debug: true,
              hideNav: '.pagination',
              outlayer: msnry,
            });
    </script>

    I’m still getting the same issue as before.
    Looks like I can’t upload custom themes at WordPress.com without upgrading.
    I’ll share it via Laragon.

    • This reply was modified 3 years, 8 months ago by hifumi.
    • This reply was modified 3 years, 8 months ago by hifumi.
    • This reply was modified 3 years, 8 months ago by hifumi.
    Thread Starter hifumi

    (@hifumi)

    I’ve set up a staging area: f1d10ac33a61.ngrok.io/blog/ I’ve made 6 posts, so when you try to scroll down (or scale the window small) it’ll attempt to fetch the rest of the previous blogs on the 2nd page, but it just floats and overlaps on the bottom left corner.

    Thread Starter hifumi

    (@hifumi)

    I’ve solved why the new appended items stack on top of each other, I have columnWidth: '.size-masonry' which is nowhere to be found in the HTML, so I removed it and it’s no longer overlapping anymore. I’ve also removed the .masonry that’s encasing the get-section and changed it to the one in the get-section file.

    The problem now is that I’ve made a msnry.js file and added:

    var msnry = document.querySelector('.masonry');
        var msnry = new Masonry('.masonry', {
            itemSelector: '.item-masonry',
    });
    var infScroll = document.querySelector('.masonry');
        var infScroll = new InfiniteScroll( '.masonry', {
        path: 'page/{{#}}',
        append: '.item-masonry',
        debug: true,
        hideNav: '.pagination',
        outlayer: msnry
    });

    and I’ve enqueued it in the function:

    function enqueue_masonry() {
            wp_enqueue_script( 'msnry', get_template_directory_uri() . '/js/msnry.js', array( 'jquery' ), '', true );
    }
    add_action( 'wp_enqueue_scripts', 'enqueue_masonry');

    but it’s giving me the error:
    Uncaught ReferenceError: Masonry is not defined
    which highlights this section:
    var msnry = new Masonry('.masonry', {

    It works fine if I put it in the index page with the script tags but I don’t want to do that.

    • This reply was modified 3 years, 8 months ago by hifumi.
    Thread Starter hifumi

    (@hifumi)

    Well I’m not sure how to get it to work in the .js file with vanilla js since the function is calling the file correctly, but I guess I’ll just leave it on the footer like that.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Infinite-Scroll requests numerical page that doesn’t exist in Masonry’ is closed to new replies.