douhao7677 2016-09-12 22:45
浏览 50
已采纳

Wordpress在鼠标滚轮上加载下一页

I want to implement this page in Wordpress: http://www.spendeeapp.com/
Basically, the loading next post on mousescroll functionality

I can replicate the mousescroll action thru jQuery

$(document).ready(function(){
    $(document)
        .on('mousewheel DOMMouseScroll swipedown swipeup', function(){
            // Do something
        })
});

PHP:

<div id="content">
            <h1>Main Area</h1>
            <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
                <h1><?php the_title(); ?></h1>
                <h4>Posted on <?php the_time('F jS, Y') ?></h4>
                <p><?php the_content(__('(more...)')); ?></p>
                <hr> <?php endwhile; else: ?>
                <p><?php _e('Sorry, no posts matched your criteria.'); ?></p><?php endif; ?>
        </div>

My question is, how can I load the next post thru the event, and display the loaded post.

  • 写回答

1条回答 默认 最新

  • donglengyuan6826 2016-09-13 05:43
    关注

    You are already halfway there. All you need to do next is create an ajax request and load the next page by using a php callback. The code will look something like this. Pay special attention to comments.

    jQuery

    $.ajax({
        url: data.ajax_url, // Localize this variable using Wordpress functions
        type: 'post',
        data: {
            action: 'load_nextpage_page', // The name of php callback (function)
            // Any other variables you may wish to pass
        },
        success: function(data) {
            console.log(data);
            // Here you can use the data returned by you PHP callback
        }
    });
    

    PHP

    <?php 
    
    // your-javascript-file-handle is the one you use while enqueuing your JS files in wordpress. Use the same handle in following code
    // to know more about localization visit https://developer.wordpress.org/reference/functions/wp_localize_script/
    
    wp_localize_script('your-javascript-file-handle', 'data', array(
        'ajax_url' => admin_url('admin-ajax.php'),
    ));
    
    
    // AJAX callback handling
    add_action('wp_ajax_load_nextpage_page', 'load_nextpage_page');
    add_action('wp_ajax_nopriv_load_nextpage_page', 'load_nextpage_page');
    
    function load_nextpage_page()
    {
        // Code for fetching next post here
        // Remember you will need a variable to keep track of which post should be fetched next.
    
    
        // Once you have fetched the right post
        //  you can echo the contents which will be passed as a reposnse to your ajax request.
    }
    

    I have outlined everything you might need to build this thing. I hope it helps you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了