仅在单个帖子上显示通知

时间:2016-12-18 作者:siam

我有一个功能,当没有访问权限的用户尝试查看单个帖子时,我会使用它显示登录通知。该功能还可以在存档、小部件等上公开显示帖子摘录,但仅在查看完整帖子时显示登录通知,即。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 -->

2 个回复
SO网友:Linnea Huxford

我认为解决方法是检查它是否是主要查询。WordPress有一个功能,is_main_query()

您在相关帖子小部件中运行的查询是一个辅助查询,因此is_main_query() 将返回false。

您可以通过如下方式更改代码来利用这一点:

if ( ! is_main_query() || ! is_singular() ) return $content;

SO网友:Eddie Padin

从代码的外观来看,您使用的是博客设计器Pro插件。

检查admin/functions.php. 有一个函数可以处理相关的POST输出。他们使用get_the_content 对于$excerpt 变量把它改成get_the_excerpt 你的功能应该正常。

相关推荐

显示作者姓名PHP(自制插件)

我有一个需要帮助的问题,因为我自己找不到解决办法。我接管了一个网站,之前有人在那里创建了一个自制插件。。使用默认插件“Contact Form 7”,用户可以在页面上创建帖子。()https://gyazo.com/c8b20adecacd90fb9bfe72ad2138a980 )关于自行创建的插件“Contact Form 7 extender”,帖子是通过PHP代码在后台生成的(https://gyazo.com/115a6c7c9afafd2970b66fd421ca76a3)其工作原理如下:如果