我怎样才能让只有会员才能在分类页面上看到帖子摘要?目前只有成员可以访问它们,但它们仍然显示在非成员的类别页面上。
注意:我现在为此制作了一个插件,您可以访问:https://github.com/NerdOfLinux/MemberOnly
我怎样才能让只有会员才能在分类页面上看到帖子摘要?目前只有成员可以访问它们,但它们仍然显示在非成员的类别页面上。
注意:我现在为此制作了一个插件,您可以访问:https://github.com/NerdOfLinux/MemberOnly
我还没来得及测试这个,但你可以用the_content
过滤器挂钩。这样,您就不必将此代码插入archive.php
, category.php
, tag.php
, author.php
以及任何其他存档文件,具体取决于主题的结构。
在您的主题中functions.php
文件添加以下内容:
function my_filter( $content ) {
$categories = array(
\'news\',
\'opinions\',
\'sports\',
\'other\',
);
if ( in_category( $categories ) ) {
if ( is_logged_in() ) {
return $content;
} else {
$content = \'<p>Sorry, this post is only available to members</p>\';
return $content;
}
} else {
return $content;
}
}
add_filter( \'the_content\', \'my_filter\' );
因此,这可以做到的是在从数据库中提取帖子和显示帖子之间拦截帖子的内容。然后,它将检查该帖子是否有$categories
数组(注意:最好在数组中使用类别slug)。然后,它将查看用户是否已登录。如果是,则会显示帖子。如果没有,则会将内容设置为消息,然后将其发送回帖子内容的位置。
如果用户已登录,或者帖子不在数组中的任何类别中,那么它将正常返回帖子。
注意:如果您使用the_excerpt()
在您的主题中,而不是the_content()
, 然后只需在add_filter()
致电至\'the_excerpt\'
它应该会起作用。
对于这种情况,我更喜欢在模板中工作,而不是在functions.php
. 这样,您就不需要额外的if
语句来确定要将条件应用到的类别。可以使用将此方法应用于所有类别category.php
如果要在不同类别中显示不同的内容,请为各个类别创建模板或自定义模板。就个人而言,我发现在这类工作中使用模板可以更容易地看到我在做什么。
在您的情况下,您将创建if
语句,如上面的示例所示is_user_logged_in
在你想向你的成员透露的任何信息之前标记。然后添加else
用于在关闭语句之前用户未登录时显示的内容。
<?php if ( is_user_logged_in() ) {
the_excerpt(); // or whatever
} else {
echo \'<p>This information is for members only.</p>\'; // or whatever
}?>
您可以使用post id将它们存储在一个数组中,然后逐个显示它们。
<?php $thePostIdArray = array("28","74", "82", "92"); ?>
<?php $limit = 4 ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); $counter++; ?>
<?php if ( $counter < $limit + 1 ): ?>
<div class="post" id="post-<?php the_ID(); ?>">
<?php $post_id = $thePostIdArray[$counter-1]; ?>
<?php $queried_post = get_post($post_id); ?>
<h2><?php echo $queried_post->post_title; ?></h2>
</div>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
我正在使用相同菜单处理类别(城市)和子类别(例如:汽车、动物、房屋)。因此,现在我的类别结构如下所示:- London -- cars -- animals -- houses - Manchester --cars --animals --houses 每个子类别的slug(似乎它们必须是唯一的)的名称如下category_name + subcategory_name 看起来像london-cars, manchester-