有一件事一直困扰着我。
我收集了12个名为“产品”的帖子,这些帖子被分配了类别,比如“军事”、“卡车运输”、“建筑”和“铁路”。例如,它们中的许多是重叠的,其中产品1可能适用于军用和卡车运输,而产品2仅适用于铁路。很简单。
这就是有趣的地方。军事页面(使用一个模板)引入了所有已分配到军事类别的产品,以及相同的Trucking产品(使用不同的模板)。一切都很好,但是当我点击到Product 1时,我希望它保留它刚才所在页面的模板。
那么基本上,我应该如何根据用户进入的页面来生成single.php的格式呢?这有可能吗?
发布于 2014-06-10 03:47:23
将以下内容放入您的footer.php文件中:
global $wp_query; // Get the global query object
$post_id = $wp_query->post->ID; // Set ID to variable
setcookie('last_id', $post_id, false, '/'); // Set cookie for subsequent requests这将在当前页面上运行所有代码后设置cookie。它本质上是为下一页设置cookie。然后,您可以签入单个模板的代码:
if(isset($_COOKIE['last_id']){
$id = intval($_COOKIE['last_id']);
// Code for setting template
}发布于 2014-08-26 08:57:03
有一个Wordpress标签可以做到这一点-- wp_get_referer。我自己也在尝试解决这个问题,并在http://zoerooney.com/blog/tutorials/using-the-referring-page-in-wordpress/上找到了答案
// use the WordPress tag wp_get_referer to assign the referring URL to the variable $referer
$referer = wp_get_referer();
// check if the URL is a specific one
if ( $referer == "whatever-your-previous-page-url-is" ) {
// if it is, do something
} else {
// if it isn't, do something else
}https://stackoverflow.com/questions/24125491
复制相似问题