AJAX navigation in Wordpress - Trouble with Permal

2019-09-05 02:28发布

So here´s where I am:

I programmed a blogtheme which resembles a desk with lots of stuff on it, draggable by the user, moving elements and so on. The main part of the blog is placed within a book which categories you can browse by using pagemarkers as well as turning pages.

To give the user the impression he/she is actually using a "book" I used a complex structure in first place (concerns mostly the place of the pagemarkers):

  1. Every category uses its own template (category-4.php, category-5.php, etc.)
  2. The single.php leads to category-sensitive templates (6 categories alltogether)

When I finished I found that there was far too much traffic caused by graphical elements as they were reloaded each click and the page was incredibly slow. So I thought about an AJAX solution and finally used the ezjax-solution (http://www.fluidbyte.net/index.php?view=simple-ajax-content-changer-with-ezjax) which is basically a script that let all a hrefs with a certain class load within a certain div in the index.php. As the content of the links is loaded into the div I save the code (and this way the traffic) for the interactive background in the other template files and this way the traffic.

You can check out the running page here and see how the navigation works.

Basically that´s exactly what I wanted but it still causes some trouble:

  1. As the template files of single-posts, categories, e.g. does NOT contain the background you can´t really use permalinks to them. You can actually do that but it would look like half the page (http://www.palimpsest.eu/2011/02/01/differenzierung/). It is evidently important that visitors will get a "normal" view onto the content.

  2. For some reason some plugins that need to hook up with the ID of the post (like Sexybookmarks or Flattr) don´t work. I guess the reason is connected to the 1. problem.

Now here are my approaches and I would like to ask you which one you consider the best to take (or maybe give some inspiration about other ways to resolve the problem):

  1. Causing the single.php to ask whether you browse inline or come from an external site and then lead into two differen templates (a short one and one with all the graphical content). In this case I don´t know how to make the single.php realize from where you are coming.

  2. I thought about using AJAXed Wordpress but I am not sure if it will suit my needs. I also don´t see much documentation about the navigation module.

  3. Any other way which won´t refresh the content that is already in the temporary internet files.

I would be really grateful to get suggestions how to cope with this issue.

Best regards, Lara

1条回答
贼婆χ
2楼-- · 2019-09-05 03:27

Welcome to SO. I'm not sure if I understand what exactly you're trying to do, but here's the usual dilemma with AJAX requests for content only in WP: You want the link, when clicked load a page as usual. But you also want to use the same link to load the same page via AJAX and then you don't want the layout to be loaded as well.

Assuming that this is the core of your problem, here's how I handle this usually:

// functions.php
function is_ajax_request() {
  return (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) &&   
          strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
}


// e.g. in index.php or any other template file:
<?php if (!is_ajax_request()) { get_header(); }  ?>

// ... Rest of your template ...

<?php if (!is_ajax_request()) { get_footer(); } ?>

Like this only the content is loaded if the template file is being requested via AJAX, otherwise the page loads just as usual.

Let me know if this helps you out, or otherwise explain a little further what you're after. And I've noticed that people around here shy away from too long questions. So — the shorter the better ;)

查看更多
登录 后发表回答