• jimjam

    (@jimjam)


    I want to have a public site but have some password protected posts. However i want the Teaser/Excerpt of the post to be read by anyone. Is this possible. Thanks in advance.

Viewing 2 replies - 1 through 2 (of 2 total)
  • I recently had to do this for a client. I added the following code to my theme’s custom functions file, and it works pretty well. (Your theme may not have this file; it should be called functions.php and should be located in the same folder as your style.css file.)

    —-

    <?php
    
        function display_protected_excerpts($excerpt)
        {
            global $post;
            if (!empty($post->post_password)) {
                $output = $post->post_excerpt;
                $output = apply_filters('get_the_excerpt', $output);
                return $output;
            }
            return $excerpt;
        }
        add_filter('the_excerpt','display_protected_excerpts', 0);
    
        function display_excerpt_on_protected_posts($content)
        {
            global $post;
            $replacement_text = $post->post_excerpt;
            $replacement_text = apply_filters('get_the_excerpt', $replacement_text);
            return str_replace('This post is password protected. To view it please enter your password below:',$replacement_text, $content);
        }
        add_filter('the_content','display_excerpt_on_protected_posts', 10);
    
    ?>

    —-

    This adds a filter that replaces the default protected messaging on the post page with the post’s excerpt. This assumes the text is the English version; does anyone know a way to internationalize this?

    I tried this with my functions.php file, and couldn’t get it to work. I have already changed the default text for my protected posts, and would like to keep the new text, but also show an excerpt. I am having no luck getting it to work. Any help would be greatly appreciated.

    You can view my protected post text at http://hampton-family.com/Yvette/?p=161. I would like to keep this text, but display an excerpt below it. preferably, followed by another message, to the tune of “this is just an excerpt, please log in to view the whole post”.

    Here is my current functions.php file – Thanks to Otto42 for the help on this one.

    <?php
    if ( function_exists('register_sidebar') )
        register_sidebar(array(
            'before_widget' => '<div class="block">',
            'after_widget' => '</div>',
            'before_title' => '<h3 class="widgettitle">',
            'after_title' => '</h3>',
        ));
    
    function change_pw_text($content) {
    $content = str_replace(
    'This post is password protected. To view it please enter your password below:',
    '<br /><b><font color="red">***This post is password protected. To view it please enter your password below.***</font></b><br /><br />
    To get your password, subscribe to our newsletter. Click the <b>*FREE Newsletter and Expert Tips*</b> link at the top of this page. You will be granted <b>VIP Access</b> to all of our content.',
    $content);
    return $content;
    }
    add_filter('the_content','change_pw_text');
    
    ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Show Teaser on Password protected posts’ is closed to new replies.