在您的single.php 模板文件:
<?php
if (has_post()) {
the_post();
$user = wp_get_current_user();
$hasPermission = in_array(\'subscriber\', $user->roles);
// or maybe instead of a role, you can check for your custom permission:
// $hasPermission = current_user_can(\'view_access_codes\');
$postTime = get_post_time(\'U\', true);
$timeThreshold = time() - 60 * 60 * 1.5;
$hasTimePassed = $postTime < $timeThreshold;
if (!$hasPermission && $hasTimePassed) {
status_header(\\WP_Http::FORBIDDEN);
?>
Only registered users can view this page.
<?php
} else {
// print the post
}
}
或使用PHP
DateTime object:
$postDate = new \\DateTime(get_post_time(DATE_W3C));
$postDate->modify(\'+90 minutes\');
$hasTimePassed = $postDate < new \\DateTime();
请注意,该帖子仍然可以通过其他方式访问,例如
the REST API 或者你的
RSS feed. 对于这些,我建议使用
the_content filter.