个性化阅读
专注于IT技术分析

WordPress-在transient中存储URL不是常量

点击下载

我需要暂时存储当前URL, 以便可以稍后使用(当我需要从搜索页面链接回该页面时)。

代码如下:

if(!is_page_template('search.php')) {
    set_transient( 'last_url', $current_url, 60*60 );
}

因此, 此代码应保存当前页面的当前URL, 直到我们进入搜索页面为止。

但是, 一旦单击搜索页面, ” last_url”将变为domain.tld / search。我不知道为什么当我明确拥有规则if(!is_page_template(‘search.php’))时会发生这种情况

但是, 我的临时解决方案是检查URL中是否也有单词搜索, 然后再创建瞬态, 例如:

if(!is_page_template('search.php')) {
    if(stripos($current_url, 'search') === false) {
        set_transient( 'shop_last_url', $current_url, 60*60 );
    }
}

虽然此解决方案有效, 但它是不好的解决方案, 因为搜索页面具有不同的特性-例如, 如果有多种语言…

我也尝试过使用cookie和会话, 但没有任何运气。


#1


如果你的主题未使用默认的WordPress查询($ wp_query), 则is_page_template, get_page_template_slug等功能将无法正常使用。

你可以在此处的相应核心代码中看到它。

因此, 对于你当前的情况, 你可以改为使用全局模板变量。

if (basename($GLOBALS["template"])=='search.php'){
 set_transient( 'last_url', $current_url, 60*60 );
}

or

if (basename(get_page_template())=='search.php'){
     set_transient( 'last_url', $current_url, 60*60 );
    }
赞(0)
未经允许不得转载:srcmini » WordPress-在transient中存储URL不是常量

评论 抢沙发

评论前必须登录!