我有一个功能,当没有访问权限的用户尝试查看单个帖子时,我会使用它显示登录通知。该功能还可以在存档、小部件等上公开显示帖子摘录,但仅在查看完整帖子时显示登录通知,即。is_singular
这基本上允许没有访问权限的用户在存档页面上查看帖子的片段,但当他们单击&;尝试查看完整帖子,系统会提示他们登录或注册。
我遇到的问题是,当使用相关帖子小部件时,会在不应该的时候显示登录通知。我很确定这与我如何在函数中定义post变量有关,这导致它无法区分小部件中显示为摘录的帖子与完整的单个帖子。寻找一些见解。
Content Protection function:
/*Show Excerpt for Protected Content*/
//Show mm content even if the user doesn\'t have access
function customContentProtection($data)
{
error_log(\'customContentProtection\');
return true;
}
add_filter(\'mm_bypass_content_protection\', \'customContentProtection\');
//then we add the filters for the archive content
add_filter( \'the_content\', \'sbm_mm_content_filter\' );
function sbm_mm_content_filter( $content ) {
global $post;
global $post_id;
if ( !is_singular())
return $content;
$new_content = $content;
// define what users without access view
if ( mm_access_decision(array("access"=>"true")) != true ){
//Need to apply a filter to get the excerpt if the template doesn\'t include it
$new_content = apply_filters(\'the_excerpt\', get_post_field(\'post_excerpt\', $post_id));
/*
//This is where we output the excerpt & login form
echo \'<div>\' . $new_content . \'</div><br><div>\' . do_shortcode(\'[mm-form-c form="red" url="/member-registration"]\') .\'</div>\';
*/
//Instead of the excerpt let\'s output a blurred text image
$url = site_url();
echo
\'<div class="no-access-wrapper">
<div class="no-access">
<img src=" \' . $url . \'/wp-content/themes/splash-child/images/blurred-text.png"></div>
<div class="no-access-top">
<h3 style="text-align:center;">This content is for members only. Please login or sign up.</h3>
\' . do_shortcode(\'[mm-form-c form="red" url="/member-registration"]\') . \'
<br>
<p style="text-align:center;"> If you are already logged in and are having trouble viewing this content, please <a href="mailto: [email protected]">contact support</a></p>
</div>
</div>\';
}
else return $new_content;
}
这是我单曲的部分。输出相关帖子小部件的php文件:
<?php
}
$related_post_number = $bdp_settings[\'related_post_number\'];
$col_class = \'\';
if ($related_post_number == 2) {
$post_perpage = 2;
}
if ($related_post_number == 3) {
$post_perpage = 3;
}
if ($related_post_number == 4) {
$post_perpage = 4;
}
$related_post_by = $bdp_settings[\'related_post_by\'];
$title = $bdp_settings[\'related_post_title\'];
if (isset($bdp_settings[\'display_related_post\']) && $bdp_settings[\'display_related_post\'] == 1) {
$related_post_content_length = isset($bdp_settings[\'related_post_content_length\']) ? $bdp_settings[\'related_post_content_length\'] : \'\';
$args = array();
if ($related_post_by == "category") {
global $post;
$categories = get_the_category($post->ID);
if ($categories) {
$category_ids = array();
foreach ($categories as $individual_category)
$category_ids[] = $individual_category->term_id;
$args = array(
\'category__in\' => $category_ids,
\'post__not_in\' => array($post->ID),
\'posts_per_page\' => $post_perpage // Number of related posts that will be displayed. \'caller_get_posts\' => 1,
);
}
} elseif ($related_post_by == "tag") {
global $post;
$tags = wp_get_post_tags($post->ID);
if ($tags) {
$tag_ids = array();
foreach ($tags as $individual_tag)
$tag_ids[] = $individual_tag->term_id;
$args = array(
\'tag__in\' => $tag_ids,
\'post__not_in\' => array($post->ID),
\'posts_per_page\' => $post_perpage // Number of related posts to display.
);
}
}
$my_query = new wp_query($args);
if ($my_query->have_posts()) {
?>
<div class="related_post_wrap">
<?php
do_action(\'bdp_related_post_detail\', $theme, $post_perpage, $related_post_by, $title, $related_post_content_length);
?>
</div>
<?php
}
}
// If comments are open or we have at least one comment, load up the comment template.
if ($bdp_settings[\'display_comment\'] == 1) {
if (comments_open() || get_comments_number()) {
comments_template();
}
}
// End of the loop.
endwhile;
if ($theme == "offer_blog" || $theme == "winter") {
echo \'</div>\';
}
?>
</div>
<?php do_action(\'bdp_after_single_page\'); ?>
</main><!-- .site-main -->
</div><!-- .content-area -->