如何在wordpress中限制页面。例如:未登录的用户[]可以查看游戏列表的5。[example.com/game/]点击“查看更多”后,user must login/register 之后,用户can access full/100 game list. [example.com/game/]
有谁知道不用插件就可以做到?非常感谢。
如何在wordpress中限制页面。例如:未登录的用户[]可以查看游戏列表的5。[example.com/game/]点击“查看更多”后,user must login/register 之后,用户can access full/100 game list. [example.com/game/]
有谁知道不用插件就可以做到?非常感谢。
使用一个短代码可以很容易地做到这一点。钩入init
并在挂钩函数中添加短代码。
<?php
add_action(\'init\', \'wpse57819_add_shortcode\');
/**
* Adds the shortcode
*
* @uses add_shortcode
* @return null
*/
function wpse57819_add_shortcode()
{
add_shortcode(\'restricted\', \'wpse57819_shortcode_cb\');
}
然后在回调函数中,可以检查用户是否登录。如果是,向他们展示内容。如果没有,则向他们显示登录消息。你可以在这里做任何你想做的事情:检查用户的能力以向他们显示内容(不同的“成员级别”),向他们显示整个登录表单。一个简单的例子:<?php
/**
* Callback function for the shortcode. Checks if a user is logged in. If they
* are, display the content. If not, show them a link to the login form.
*
* @return string
*/
function wpse57819_shortcode_cb($args, $content=null)
{
// if the user is logged in just show them the content. You could check
// rolls and capabilities here if you wanted as well
if(is_user_logged_in())
return $content;
// If we\'re here, they aren\'t logged in, show them a message
$defaults = array(
// message show to non-logged in users
\'msg\' => __(\'You must login to see this content.\', \'wpse57819\'),
// Login page link
\'link\' => site_url(\'wp-login.php\'),
// login link anchor text
\'anchor\' => __(\'Login.\', \'wpse57819\')
);
$args = wp_parse_args($args, $defaults);
$msg = sprintf(
\'<aside class="login-warning">%s <a href="%s">%s</a></aside>\',
esc_html($args[\'msg\']),
esc_url($args[\'link\']),
esc_html($args[\'anchor\'])
);
return $msg;
}
作为plugin.Usage
在您的页面/帖子中的某处:[restricted]
Content for members only goes here
[/restricted]
可能是一个自定义的短代码。请参阅此插件http://wordpress.org/extend/plugins/restrictedarea它已经过时了,但您应该为您的pourpose使用该代码
首先,您需要添加一个自定义元框,允许您将帖子标记为隐藏。
有关详细信息,请单击original answer this source link
我修改此功能以满足您的需要
add_action( \'pre_get_posts\', \'yourtextdomain_pre_get_posts_hidden\', 9999 );
function yourtextdomain_pre_get_posts_hidden( $query )
{
// Check if on frontend and main query.
if( ! is_admin() && $query->is_main_query() )
{
if( ! is_user_logged_in() )
{
// For the posts we want to exclude.
$exclude = array();
// Locate our posts marked as hidden.
$hidden = get_posts(array(
\'post_type\' => \'post\',
\'meta_query\' => array(
array(
\'key\' => \'meta-box-checkbox\',
\'value\' => \'true\',
\'compare\' => \'==\',
),
)
));
// Create an array of hidden posts.
foreach($hidden as $hide)
{
$exclude[] = $hide->ID;
}
// Exclude the hidden posts.
$query->set(\'post__not_in\', $exclude);
}
}
}