仅为非注册用户显示博客文章的前300字/50行

时间:2021-09-29 作者:Arquera

是否可以只为匿名用户显示每个博客帖子的前300个单词(甚至50行)?而不是在页面上显示整篇文章?注册后,所有帖子都会显示出来。

谢谢

2 个回复
SO网友:Pixelsmith

这并不像帕特·J说的那么简单,尤其是如果你愿意的话HTML 使用副本格式化。我抄袭了this answer 并为您提供了以下代码。我已经对它进行了测试,它可以正常工作:

    // If the user is logged in, display the full content
    if(is_user_logged_in()):
        the_content();
    else: // The user isn\'t logged in and should only see the first 300 words
        echo force_balance_tags( html_entity_decode( wp_trim_words( htmlentities( wpautop(get_the_content()) ), 300, \'...\' ) ) );
    endif;

UPDATE

您的代码中有2个错误。一个是; 在初始if 声明,以及arrays 不匹配:$roles v$role.

以下代码将原始答案(登录/注销)与您的修改(如果是阵列)集成在一起:

    if(is_user_logged_in()): 
        if( in_array( \'administrator\', $roles ) || in_array( \'pmpro_role_2\', $roles ) || in_array( \'pmpro_role_1\', $roles )): 
            the_content();
        else: 
            echo force_balance_tags( html_entity_decode( wp_trim_words( htmlentities( wpautop(get_the_content()) ), 300, \'...\' ) ) );
        endif; 
     else:
        echo force_balance_tags( html_entity_decode( wp_trim_words( htmlentities( wpautop(get_the_content()) ), 100, \'...\' ) ) );
     endif;
祝你好运!

SO网友:Alexandro Giles

您可以使用摘录来实现这一点,但摘录在默认情况下会过滤所有HTML标记。要解决这个问题,我们需要对其进行过滤并添加新的逻辑。

这将根据您的需要过滤摘录以进行更改:

您必须在主题中添加此功能functions.php

functions.php

function wpse_allowedtags() {
    //The Tags you put here will not be removed, therefore if you want to include strong HTML tag, you should add it here as shown:
        return \'<strong>\'; 
    }

if ( ! function_exists( \'wpse_custom_wp_trim_excerpt\' ) ) : 

    function wpse_custom_wp_trim_excerpt($wpse_excerpt) {
    $raw_excerpt = $wpse_excerpt;
        if ( \'\' == $wpse_excerpt ) {

            $wpse_excerpt = get_the_content(\'\');
            $wpse_excerpt = strip_shortcodes( $wpse_excerpt );
            $wpse_excerpt = apply_filters(\'the_content\', $wpse_excerpt);
            $wpse_excerpt = str_replace(\']]>\', \']]&gt;\', $wpse_excerpt);
            $wpse_excerpt = strip_tags($wpse_excerpt, wpse_allowedtags()); /*IF you need to allow just certain tags. Delete if all tags are allowed */

            /*Set the excerpt word count and only break after sentence is complete.
            You can set this to any number you want in this case we\'ll use 300 
            words. */
                $excerpt_word_count = 300;
                $excerpt_length = apply_filters(\'excerpt_length\', $excerpt_word_count); 
                $tokens = array();
                $excerptOutput = \'\';
                $count = 0;

                // Divide the string into tokens; HTML tags, or words, followed by any whitespace in order to clean the not allowed HTML tags.
                preg_match_all(\'/(<[^>]+>|[^<>\\s]+)\\s*/u\', $wpse_excerpt, $tokens);

                foreach ($tokens[0] as $token) { 

                    if ($count >= $excerpt_length && preg_match(\'/[\\,\\;\\?\\.\\!]\\s*$/uS\', $token)) { 
                    // Limit reached, continue until , ; ? . or ! occur at the end
                        $excerptOutput .= trim($token);
                        break;
                    }

                    // Add words to complete sentence
                    $count++;

                    // Append what\'s left of the token
                    $excerptOutput .= $token;
                }

            $wpse_excerpt = trim(force_balance_tags($excerptOutput));

            if ( $count <  0) {   
                $excerpt_end = \' <a href="\'. esc_url( get_permalink() ) . \'">\' . \'&nbsp;&raquo;&nbsp;\' . sprintf(__( \'Reed more: %s &nbsp;&raquo;\', \'wpse\' ), get_the_title()) . \'</a>\'; 
               $excerpt_more = apply_filters(\'excerpt_more\', \' \' . $excerpt_end); 
               $wpse_excerpt .= $excerpt_more; /*Add read more in new paragraph */
           }  
           $wpse_excerpt .= \' [...]\';

            return $wpse_excerpt;   

        }
        return apply_filters(\'wpse_custom_wp_trim_excerpt\', $wpse_excerpt, $raw_excerpt);
    }

endif; 

remove_filter(\'get_the_excerpt\', \'wp_trim_excerpt\');
add_filter(\'get_the_excerpt\', \'wpse_custom_wp_trim_excerpt\'); 
之后,您只需调用主题所需模板文件中的非注册用户的\\u摘录:(请记住,它之前已过滤为包含300个单词)

 // The user is logged in, display the full content
    if(is_user_logged_in()){
       the_content();
     }else{
        // show the_excerpt
        <?php echo get_the_excerpt() ?>
     }
        

相关推荐

您是否可以使用GET_USERS()返回比当前用户更高角色的用户?

我使用get\\u users()返回具有指定角色的用户列表。这样做的目的是在前端生成一个下拉列表,在评论中提及其他用户,这与https://wordpress.org/plugins/comment-mention/.问题是,如果当前用户处于较低的角色(如作者),则get\\u users()不会返回较高的角色(如管理员)。换句话说,我需要一个较低的用户,以便能够返回具有较高角色的用户。我意识到get\\u users()阻止从此处返回更高角色的用户:get_users / WP_User_Query