Wordpress - single.php 调用了两次 [英] Wordpress - single.php called twice

查看:21
本文介绍了Wordpress - single.php 调用了两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个插件,为了一些快速而肮脏的开发,我添加了一个对 single.php 的函数调用,该函数调用我的插件中的一个函数,该函数将有关当前帖子的数据存储在数据库中.

I am working on a plugin and for some quick and dirty development I have added a function call to single.php that calls a function in my plugin which stores data about the current post in the db.

不过,我注意到它总是被调用两次:一次用于我请求的帖子,一次用于另一篇帖子(未显示在页面内容中).

I have noticed, though, that it is always called twice: once for the post I am requesting and once for another post (which does not show up in the content of the page).

有谁知道这是为什么,如果是,是否是期望的行为,如果不是,我该如何解决?

Does anyone know why this is and, if so, whether it's desired behaviour and how I can fix it if not?

这是一个类似于我的 single.php 的例子:

Here's an example similar to my single.php:

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
                   <?php do_action('msw_single_php', $wp_query);  ?>

还有一个类似于我的插件文件:

And one similar to my plugin file:

add_action('msw_single_php', 'msw_sp', 0, 1);
function msw_sp(WP_Query $query) {
    global $wpdb;
    $wpdb->insert($wpdb->prefix . 'msw_career_view_debug', array(
        'request_info'      => json_encode($query),
    ));
}

推荐答案

我遇到了同样的问题,现在已经解决了.

I ran into the same issue and now it's SOLVED.

对我来说,这个问题只发生在 Firefox 上,当调用 single.php 时.而你不想要的帖子是相邻的.

For me, the issue only happened on Firefox, when single.php was called. And the post which you didn't want was the adjacent one.

然后我搜索了很多,终于在这里找到了解决方案:

I then searched a lot and finally found the solution here:

http://shinephp.com/single-php-call-两次如何停止/

它给出了另一个参考:

http://wordpress.org/support/topic/singlephp-Called-for-current-and-next-post-with-pretty-permalinks

感谢以上链接.

我想在这里做一个简短的说明:

I'd like to put a brief here:

原因是 Firefox 预取功能.对于single.php,WP自动放两个;在 <header> 中,其中一个是 rel="next",它指向下一个帖子(相邻帖子).

The reason is Firefox prefetch feature. For single.php, WP automatically puts two <link> in <header>, and one of them is rel="next", which points to the next post (adjacent post).

当 Firefox 看到这个时,它会预取 <link> 指向的页面.为了加快速度,以防您在阅读当前帖子后转到下一个帖子.因此,该帖子将被调用,但您将永远不会看到它显示出来.这也是为什么您看不到为调试而转储的任何消息的原因,甚至看不到回溯.

When Firefox sees this, it'll prefetch the page pointed to by the <link> in order to speed up in case you go to the next post after reading the current one. And thus that post will be called but you will never see it displayed. This is also why you can't see any message you dump for debugging, not even backtrace.

顺便说一下,添加两个 的操作.添加在default-filters.php"中.对于 WP 3.5.1,它位于 #204 行.

BTW the action which adds the two <link> is added in "default-filters.php". For WP 3.5.1 it's in line #204.

解决方案是:

编写一个执行的函数(例如remove_adjacent_links"):

Write a function (for example "remove_adjacent_links") which executes:

remove_action(‘wp_head’, ‘adjacent_posts_rel_link_wp_head’)

并将函数 (remove_adjacent_links) 挂接到init".

and hook the function (remove_adjacent_links) to "init".

这样,链接将从中删除.

This way, the links will be removed from .

但是,如果您确实希望链接在那里,也许您必须保持这种方式并容忍 Firefox 的预取...这就是我正在做的事情,因为这对我的情况无关紧要.我没有尝试是否有办法离开那里并使用WP代码阻止Firefox预取.

However if you do want the links to be there, maybe you'll have to leave it this way and tolerate Firefox's prefetch... this is what I'm doing because it doesn't matter for my case. I didn't try whether there is a way to leave the there and use WP code to stop Firefox from prefetching.

这篇关于Wordpress - single.php 调用了两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆