[s_ha_dum 在 http://wordpress.stackexchange.com 上的解决方案]
我正在尝试根据在帖子设置中输入的密码将用户引导至某个帖子。我几乎有工作代码:
页面表格:
<form method="post" action="">
<input type="password" name="passwordfield">
<input type="hidden" name="homepagepassword" value="1">
<input type="submit" value="Submit">
</form>
functions.php 中的代码
function doPasswordStuff(){
if(isset($_POST['homepagepassword'])){
$post_password = $_POST['passwordfield'];
$post_id = $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_password = %s", $post_password) );
$q = new WP_Query( 'p=$post_id' );
if($q->have_posts()){
while($q->have_posts()){
$q->the_post();
wp_redirect(get_permalink());
die();
}
} else {
// oh dear, there isnt a post with this 'password', put a redirect to a fallback here
wp_redirect('http://www.google.com');
die();
}
wp_reset_query();
}
}
add_action('init','doPasswordStuff');
但是我在这一行遇到致命错误(致命错误:调用非对象上的成员函数 get_var()):
$post_id = $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_password = %d", $post_password) );
如果有人愿意看一下真的很感激:)谢谢!