• Resolved janvitos

    (@janvitos)


    Hi,

    I might be close to getting the auto load recent / related posts working, but I think I’m missing something about the alnp-post-url trigger.

    Here’s the code I have now:

    
    	$.ajax(
        {
            type: "post",
            dataType: "json",
            url: ajax_object.ajax_url,
            data: { 'action': 'load_recent' },
            success: function(response){
    			$('body').on( 'alnp-post-url', function( e, post_count, post_url ) {
    				$.each(response, function (index, value) {
    					post_count = index + 1;
    					post_url = JSON.stringify(value);
    				});
    			});
            },
        });
    

    So basically, I’m using an Ajax call to return recent post URLs from a php function. That part is working, but when calling the alnp-post-url trigger using those URLs, the normal posts are still being loaded and not the ones returned by my ajax call.

    Any help would be appreciated.

    Thank you.

    • This topic was modified 6 years, 9 months ago by janvitos.
    • This topic was modified 6 years, 9 months ago by janvitos.
    • This topic was modified 6 years, 9 months ago by janvitos.
Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author Sébastien Dumont

    (@sebd86)

    The trigger is designed to return a single post URL only. Auto Load Next Post is not designed to preload posts.

    Thread Starter janvitos

    (@janvitos)

    I understand.

    If possible, could you please show me an example of a working alnp-post-url trigger that changes the URL for the first returned post? There is no example in the docs. I thought my code would work, but it seems I’m missing something.

    Thank you.

    Thread Starter janvitos

    (@janvitos)

    Alright.

    I got my code working by changing the prev navigation link to the URLs I fetched from my Ajax call: $(‘nav.post-navigation’).find( ‘a[rel=”prev”]’).attr( ‘href’, myajax[myvar] );

    But I also had to modify auto-load-next-post.js and move this line: jQuery( ‘body’ ).trigger( ‘alnp-post-url’, [ post_count, post_url ] );

    Above this line: var post_url = jQuery( nav_container ).find( ‘a[rel=”prev”]’).attr( ‘href’ );

    I had to do this to get it working because otherwise, the post_url variable had already been set before I could change the nagivation link.

    With these changes, I’m able to return recent posts and related posts (using the related posts function from the elasticpress plugin).

    I’m not sure if moving the line in the auto-load-next-post.js file is a bug fix or not, but that’s the only way I could get it working.

    • This reply was modified 6 years, 9 months ago by janvitos.
    • This reply was modified 6 years, 9 months ago by janvitos.
    Thread Starter janvitos

    (@janvitos)

    Btw, I can show you a working example if you like. I could also provide the PHP code if you want.

    • This reply was modified 6 years, 9 months ago by janvitos.
    Plugin Author Sébastien Dumont

    (@sebd86)

    Thank you for your feedback @janvitos. It came in handy just before I put out my next beta release. After further testing for overriding the post url it did not at all want to update the variable so in the coming release I have added a new variable which will store the new post url temporarly and then update the post url variable before loading that post.

    I have now also added a code snippet. https://github.com/AutoLoadNextPost/alnp-code-snippet-library/blob/master/snippets/alnp-override-post-url.js

    I don’t know why this particular trigger would not return and update the variable already there. The other triggers appear to be working with no issues.

    I am interested in what you have done with elasticpress plugin.

    If you are willing to share your code via a private Gist https://gist.github.com/, that would be great.

    Thank you.

    Thread Starter janvitos

    (@janvitos)

    Hi Sebastien, glad I could help.

    Here is the private Gist for Auto Load Recent Posts:

    https://gist.github.com/janvitos/b9c865cd1020b1a0fc85ea4384ad88ad

    Here is the private Gist for Auto Load Related Posts (with ElasticPress plugin):

    https://gist.github.com/janvitos/5b7538ba168f9a3fd35f560f1c902c15

    If you have any questions, feel free to ask.

    Thread Starter janvitos

    (@janvitos)

    Please note that for the time being, the two Gist above will only work if you modify auto-load-next-post.js and move this line: jQuery( ‘body’ ).trigger( ‘alnp-post-url’, [ post_count, post_url ] );

    Above this line: var post_url = jQuery( nav_container ).find( ‘a[rel=”prev”]’).attr( ‘href’ );

    • This reply was modified 6 years, 9 months ago by janvitos.
    • This reply was modified 6 years, 9 months ago by janvitos.
    Plugin Author Sébastien Dumont

    (@sebd86)

    With the change I have made in the coming release your feature will work without modifying the plugin directly.

    It will just need a little tweaking. I will document the changes in the next blog post once I have the new beta release out.

    Thread Starter janvitos

    (@janvitos)

    Great. That sounds awesome.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘How to use the alnp-post-url trigger’ is closed to new replies.