让我说,我有的“我的短代码”作为一个短代码。我使用我的短代码在wordpress页面(pageid = 1)中插入了这个短代码。
如何在代码文件中获取页面的id?我搜索并得到了这个解决方案,似乎给了我身份证。
global $wpdb;
$PageID = $wpdb->get_var('SELECT ID FROM '.$table_prefix.'posts WHERE post_content LIKE "%[myshortcode]%" AND post_parent = 0');但问题是它将页面id作为字符串返回。所以我似乎不能用数字。
例如,如果我在wp_redirect中使用它,就会得到页面没有正确地重定向的错误。
我尝试了(int)$PageID,但似乎没有解决问题。
有什么帮助吗?
发布于 2016-03-19 06:36:19
我解决了。然而,我不太清楚问题出在哪里。就像我说过的,我在一个插件里工作。这是应该重定向的代码。
// This is in the plugin's Initialize function.
add_action( 'template_redirect', 'wc_custom_redirect_after_purchase' );
function wc_custom_redirect_after_purchase() {
global $wp, $wpdb;
$PageID = $wpdb->get_var('SELECT ID FROM '.$table_prefix.'posts WHERE post_content LIKE "%[myshortcode]%" AND post_parent = 0');
if ( is_checkout() && ! empty( $wp->query_vars['order-received'] ) ) {
$order_id = absint( $wp->query_vars['order-received'] );
$order_key = wc_clean( $_GET['key'] );
/**
* Replace {PAGE_ID} with the ID of your page
*/
$redirect = get_permalink($PageID);
$redirect .= get_option( 'permalink_structure' ) === '' ? '&' : '?';
$redirect .= 'order=' . $order_id . '&key=' . $order_key. '&noredirect=1';
wp_redirect( $redirect );
exit;
}
}我不知道是什么问题,但我总是得到页面重定向无休止的方式。然后我在网上冲浪,发现“页面没有正确地重定向”可能意味着您的重定向功能总是重定向到页面本身。所以这是一个永无止境的循环。其中一个原因是当有一个空值被传递给重定向函数时。
我做了var_dump($PageID)和整体性,它传递null。
你一定觉得我疯了,不早点检查一下,但我做到了。我使用了相同的代码并将其放在另一个文件中,得到了$PageID =1(正如我在问题中解释的那样)。不太清楚为什么相同的代码在我的插件文件中返回null。
我换了这条线
$PageID = $wpdb->get_var('SELECT ID FROM '.$table_prefix.'posts WHERE post_content LIKE "%[myshortcode]%" AND post_parent = 0');直接到这里
$PageID = $wpdb->get_var('SELECT ID FROM '.$wpdb->prefix.'posts WHERE post_content LIKE "%[myshortcode]%" AND post_parent = 0');是的,这次重定向是成功的。
我仍然不知道为什么以前的$PageID查询在其他文件中工作,而在我的插件文件中不工作。所以,我什么也没学到。它只是被击中和审判,我很幸运。
希望有人能指出真正的原因。在那之前,我可以很高兴地接受它。感谢任和丹尼尔·抽出时间来回应。
https://stackoverflow.com/questions/36090025
复制相似问题