把这个放在最上面page.php
文件,在get_header()
电话:
<?php
// redirect if user isn\'t logged in
if( ! is_user_logged_in() ) {
wp_safe_redirect( home_url( \'/signup/\' ) );
exit;
}
// supply IDs of both parent pages
$keys = array( \'21\', \'23\' );
// check if user can access page
foreach( $keys as $key )
{
// get parent page object
$parent = get_page( $key );
// add IDs of parent and children to array
$pageids = array();
$objs = get_pages( array( \'child_of\' => $parent->ID ) );
foreach( $objs as $obj ) $pageids[] = $obj->ID;
$pageids[] = $key;
if(
// check if current page is restricted area
in_array( get_queried_object_id(), $pageids )
// and check if current user has appropriate meta
&& \'1\' !== get_user_meta( get_current_user_id(), $parent->post_name, true )
) {
// redirect to signup page if conditions aren\'t met
wp_safe_redirect( home_url( \'/signup/?for=\' . $parent->post_name ) );
exit;
}
}
?>
注意:(a)您必须为
$keys
第9行的数组,以及(b)此代码期望用户具有具有键的用户元
community1
(和/或
community2
), 和价值
1
如果他们有权限。