Page 1 of 1

Membership only for older posts

PostPosted: November 14th, 2011, 10:11 am
by ronhen
Hello,

Is it possible to setup S2 in way that you have to be a member to see posts of say older than one month. The newest posts must always be free for every visitor. Or will that require some extra (source)coding?

Thanks
Ron

Re: Membership only for older posts

PostPosted: November 14th, 2011, 10:41 am
by Eduan
See if this video gives you an idea:
http://www.s2member.com/advanced-conditionals-video/

And you can sell access with levels, so level 0 (free) has access to newer posts and level 1 has access to everything.
Check this video on how to do that:
http://www.s2member.com/general-options-overview-video/

I found all these videos in this link:
http://www.s2member.com/videos/

Re: Membership only for older posts

PostPosted: November 14th, 2011, 10:37 pm
by Raam Dev
Hi Ronhen,

I recently did something similar with s2Member. It required modifying my single.php theme file.

What I did was check if the post was less than 30 days old. If the post is less than 30 days old *OR* the person viewing it is logged in, then display the post.

Otherwise, (if the post is older than 30 days old and the person viewing it is NOT logged in) display a message that says they need to be a member and give them a link to subscribe.

Here's an example code from my own single.php. Your theme file will look slightly different, but the concept is the same.

Code: Select all

    <div id="content">

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

        <div <?php post_class() ?> id="post-<?php the_ID(); ?>">
            <h2><?php the_title(); ?></h2>

                <?php
                $restrict_after
=30 * 86400; // days * seconds per day (30 days)
                $post_age = date('U') - get_post_time('U');
                if ($post_age < $restrict_after || is_user_logged_in()) { ?>
                    <div class="entry">

                        <?php the_content(); ?>
                                
                    </div>        

                <?php }
                else { ?>
                    <div class="entry">                    

                           <p>This post is older than 30 days and can only be accessed with a <a href="http://example.com/join /">membership</a>. If you already have a membership, please <a href="http://example.com/login/">login</a> to view this post.</p>

                    </div>
                    
        <?php } // Closes if-block that checks post age ?>    
        </div> <!-- closes 'post class' -->

    <?php endwhile; else: ?>

        <p>Sorry, no posts matched your criteria.</p>

<?php endif; ?>

    </div> <!-- closes 'content' -->


As Eduan mentioned, you can also incorporate s2Member Advanced Conditionals.

For example, my code above simply checks if the visitor is logged in (is_user_logged_in()) and then lets them view the page. If you wanted to only let people with s2Member Membership Level #2 or higher to see the post, you could change that to current_user_can("access_s2member_level2").

You could also incorporate checking for specific Custom Capabilities that you've assigned the user (see WP-Admin -> s2Member -> API / Scripting -> Custom Capabilities (Packages)).

Re: Membership only for older posts

PostPosted: November 15th, 2011, 3:01 am
by ronhen
Thanks Raam! Figured out it had to be something like that, but the code example is very helpfull.
By the way, I wonder why this membership model (newest posts free for every visitor) isn't used more offen, because it seems an option if your website is depending on ads also.