weixin_33670713 2017-06-09 21:52 采纳率: 0%
浏览 26

Wordpress,ajax加载的内容

So, I'm incredibly green with php, ajax, and anything javascript. I've put this site together through various tutorials and hackneyed methods of making things work. However, I've hit a pretty big snag with two problems revolving around URLs.

  1. I have no idea how to properly bind something using the back button to the action that happens when a post is clicked.

  2. Sub-urls go straight to the post content (single.php), instead of the index template with the content pulled up.

Now, here is my code. I'm sure there are a million things wrong with it.

(function($) {

  $(function($) {
        $(".post-link").click(function loadContent(){
            $(this).parent().addClass('fullscreen');
            $('.close').toggleClass('closeactive').removeClass('close');
            $(".back, .next").show();
            $('body').addClass('noscroll');

        });

        $(document).ready(function(){
        $.ajaxSetup({cache:false});
        $(".post-link").click(function(e){
            var post_link = $(this).attr("href");

            $(this).siblings('#single-post-container').html("content loading");
            $(this).siblings('#single-post-container').load(post_link);
            window.history.pushState('object', 'New Title', post_link);
            e.preventDefault();

        window.onpopstate = function(event) {
            $("#loading").show();
            console.log("pathname: "+location.pathname);
            loadContent(location.pathname);
            $(this).parent().removeClass('fullscreen');
        };

        return false;
        });

    });
});

Basically, what's happening is a lot of css and an ajax call. I'm not entirely sure I'm going about it right, but it functions pretty well. You can see it implemented on my site www.andbloom.com. Any help would be greatly appreciated!

  • 写回答

1条回答 默认 最新

  • weixin_33725270 2017-06-10 14:59
    关注

    First point

    You already bind the back event, with

    window.onpopstate = function(event) {
        $("#loading").show();
        console.log("pathname: "+location.pathname);
        loadContent(location.pathname);
        $(this).parent().removeClass('fullscreen');
    };
    

    You have an error, when this function is run, because loadContent function is not defined (you need to take it out of click(), and defined it before if you want to use it).
    Another thing is that you shouldn't add this code inside the $(".post-link").click(function(e){}, as you only need to bind this once.
    And last thing on this is that you don't need to bind the click event twice. See below an example with an updated code snippet. I couldn't test this in the browser, as you have the code in index (html) and not a separate JS file.

    $(function ($) {
        $(document).ready(function () {
            $.ajaxSetup({cache: false});
    
            $(".post-link").click(function (e) {
                e.preventDefault();
    
                // this was moved from the function above, as you
                // don't need two events
                $(this).parent().addClass('fullscreen');
                $('.close').toggleClass('closeactive').removeClass('close');
                $(".back, .next").show();
                $('body').addClass('noscroll');
    
                var post_link = $(this).attr("href");
    
                $(this).siblings('#single-post-container').html("content loading");
                $(this).siblings('#single-post-container').load(post_link);
                window.history.pushState('object', 'New Title', post_link);
            });
    
    
            window.onpopstate = function (event) {
                // here you should add the logic to reverse (similar to what you have on the close event)
                $(this).parent().removeClass('fullscreen');
                $('.closeactive').toggleClass('close').removeClass('closeactive');
                $(".back, .next").hide();
                $('body').removeClass('noscroll');
            };
        });
    });
    

    Second point

    This issue is a bit more complex.
    An approach here would be for you to add the same code as you have on the homepage on the single.php template as well and show that single initially via javascript. Or add a redirect on the single page to the homepage with a GET param holding the value of the single page e.g. http://www.andbloom.com/?s=funny-or-die
    No matter of your choice here, you will also need to update your ajax call to make a separate function to return only what you have(simple html) on single.php

    Other notes

    • You should move your code from inline, to a separate file and format it. It was really hard to read through it and I can't edit things, as dev tools only allow editing external JS and CSS files
    • You have multiple elements with the same ID on the page. I'm referring to single-post-container
    • You should really have only one $( document ).ready(function() {} in your case for the whole code related to this functionality.
    评论

报告相同问题?

悬赏问题

  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事: