我已经创建了一个自定义的帖子类型‘客户端’,管理员用户可以创建新的客户端,添加图片和详细信息到帖子,然后密码保护页面,以便只有一个特定的客户端可以访问内容。
为了在前端显示这个post类型的内容,我使用了一个single-clients.php模板。它完美地显示内容,但密码保护功能不显示表单和隐藏内容,即使我在不同的浏览器,缓存清除/注销Wordpress (查看它作为一个普通的最终用户)。
我在这里可能做错了什么?
<?php get_header(); ?>
<div class="container-client">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
Display all fields and content for post-type
<?php endif; ?>
<?php endwhile; else: ?>
<div class="alert-box error">Sorry, this page no longer exists :( — <a href="<?php bloginfo('url'); ?>/">Back to home</a></div>
<?php endif; ?>
</div>
<?php get_footer(); ?>
这大致就是我的单个clients.php页面的设置方式。有没有办法手动显示密码功能,以便最终用户访问页面时,隐藏内容并显示密码表单?
发布于 2014-11-13 20:15:38
我确实遇到了这个问题,经过一些尝试和阅读,我想出了这个解决方案:
<?php
add_filter('single_template', function($single_template) {
global $post;
if ($post -> post_type == 'your-custom-post-type') {
if (!post_password_required()) {
$single_template = 'path-to-your/single-your-custom-post-type.php';
}
}
return $single_template;
});
?>
这样,如果页面受密码保护,则仅在输入密码后才会在自定义sinlge视图中呈现。
https://stackoverflow.com/questions/13957184
复制相似问题