没有#!的AJAX URL,如何防止在加载或重载时落入Single.php?

时间:2012-12-15 作者:wyem

我正试图通过一个支持ajax的单页公文包来正确地处理问题。我读到没有必要#! 让它可以被谷歌抓取。所以我有干净的URL更新.pushState 当我加载一个帖子时。

我的问题是,如果你点击F5或直接加载一个URL,你就会陷入单一。php。您可以在此处亲自尝试:http://www.youpiemonday.com/

我怎样才能避免掉进文章页面?

我想从单曲中重新定向。php到主页并触发打开那里的帖子。但是发表评论,这似乎不是一个好主意。不管怎样,我现在被困住了。。。你可以看到我对StackOverflow的思考过程here.

我很乐意在这里分享Wordpress专家的一些反馈……:)

<小时>EDIT: 我可能已经找到了一种简单/快速的方法来重定向到主页,如果请求是在单身时完成的。php。我只是将其添加到函数中。php

add_action(\'wp\', \'RedirecToPost\'); 
 function RedirecToPost(){ 
    if($_SERVER[\'HTTP_X_REQUESTED_WITH\']==\'\' && is_single()){ // if http request AND on single.php 
            wp_redirect( home_url() ); // redirect to home
            // THEN check the post requested AND trigger a click on its thumbnail

        }
  }
但我仍然在同一点上。我需要一种方法来检查请求的帖子是什么,并触发对其缩略图的单击。现在我真的被我的脚本知识困住了。如何检查最后一页url(referer?)并在当前页面中找到指向它的href?

1 个回复
SO网友:Ralf912

简而言之:不要使用single.php. 如果你只有index.php, 因为WP找不到任何其他模板来显示内容,所以所有帖子都使用此模板显示。

您的人类语言问题:

如果url为www.youpiemonday.com, 加载frontpage(index.php、home.php或其他内容)

如果url为www.youpiemonday.com 使用参数,则显示参数内的单个帖子single.php.

WordPress会这么做,因为youpiemonday.com/singlepost/ 将由WP(内部)转换为www.youpiemonday.com/?p=123 (其中123 是的帖子IDsinglepost)

所以你要做的是:

每次有人打电话www.youpiemonday.com, 呈现frontpage(index.php,home.php,…)投资组合中没有一个帖子开放。But 如果url(/singlepost/)中有参数,则呈现frontpageand 打开单个帖子(如果可用)。

删除或重命名您的单曲。php强制WP使用索引。php的主题来显示各种帖子。要触发单击,可以在函数中添加开关。php

add_action( \'wp_head\', \'trigger_click_on_single_post\' );

function trigger_click_on_single_post(){

$js_template = \'

<script type="text/javascript">

jQuery( document ).ready(

  function( $ ){

  // create the js to trigger the link here
     LinktoRef = $(".ProjectWrap" ).find( "a" ).href; // the href in the thumbnail

     if ( "{{link_to_click}}" === LinktoRef ){
       find( "a" ).trigger( "click" );
     }

   }

);

</script>

\';


  if( is_single() ){

    global $post;

    $replace = get_permalink(); // create the propper url here

    $js_output = str_replace( \'{{link_to_click}}\', $replace, $js_template );

    echo $js_output;

  }
}
PS:我不知道为什么,但是WPSE上的代码格式不喜欢heredoc语法。因此,代码块中有一个小小的中断。

结束