PriMoThemes — now s2Member® (official notice)

This is now a very OLD forum system. It's in READ-ONLY mode.
All community interaction now occurs at WP Sharks™. See: new forums @ WP Sharks™

Members only latest posts page

s2Member Plugin. A Membership plugin for WordPress®.

Members only latest posts page

Postby Terry » July 26th, 2011, 7:59 am

Hello all,
First of all, thank you for such an excellent plugin! s2Member has been a fantastic addition to the site and I am extremely happy with it!
I have most of it setup, and am currently hitting a wall on how to display member only posts in the loop of a template page. I would say I am intermediate at theme creation in wp, and have created a widget in order to display a different message to members and non-members (e.g. subscribe to one and login to the other).

I know the easy way would be to display posts of a specific members only category e.g. Premium Content.
However, we will have many Authors contributing to the site within the company, and therefore I expect there will be a few that will find it difficult to adhere to the rule of posting premium content in the "Premium Content" category (as obvious as that sounds :roll: )!

With this in mind, I need to create a more dynamic method. Below is an example of the code I am using for the home-page.php
Code: Select all
<div id="main">
  <div id="left_column">
    <div class="left_col_box"> <span class="main_titles">Featured Content</span>
      <?php $args = array(
    
'posts_per_page' => 1,
    
'post__in'  => get_option'sticky_posts' ),
    
'ignore_sticky_posts' => 1
);
query_posts$args );?>
      <?php rewind_posts(); ?>
      <?php while (have_posts()) : the_post(); ?>
      <h1><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
        <?php the_title(); ?>
        </a></h1><br />

      <?php the_content('Read more &raquo;'); ?>
      <br /><div class="post_details">
 by <?php the_author() ?> Date: <time datetime="<?php the_time('Y-m-d'?>" pubdate="<?php the_time('F jS, Y'?>"><?php the_time('F jS, Y'?></time>
        <br/>
        Posted in
        <?php the_category(', '?>
        | <?php the_tags('Tags: '', ''<br />'); ?>
        <?php edit_post_link('Edit'''' | '); ?></div>
      <div class="clear"></div>

    </div>
    <?php endwhile; ?>
    <div class="left_col_box"> <span class="main_titles">Recent Content</span>
      <?php

// The Query
query_posts( array('posts_per_page=5','ignore_sticky_posts' => 1) );

// The Loop
while ( have_posts() ) : the_post(); ?>
      <h2 style="margin-bottom: 0px;"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
        <?php the_title(); ?>
        </a></h2>
      <div class="post_details">
        
        by <?php the_author() ?> Date: <time datetime="<?php the_time('Y-m-d'?>" pubdate="<?php the_time('F jS, Y'?>"><?php the_time('F jS, Y'?></time>
        <br/>
        Posted in
        <?php the_category(', '?>
        | <?php the_tags('Tags: '', ''<br />'); ?>
        <?php edit_post_link('Edit'''' | '); ?>
        
      </div>
      <?php endwhile;

// Reset Query
wp_reset_query();

?>
    </div>
  </div>
  <div id="right_column">
 <?php if ( is_active_sidebar'rcb-widget-area' ) ) : ?>

            <div class="right_col_box"> 
                <?php dynamic_sidebar'rcb-widget-area' ); ?>
            </div>

<?php endif; ?>
    
  </div>
</div>


This will show any user (logged in or not) all posts with a featured on the top, and a list on the bottom. I want to create the same layout only with Featured Premium Post on the top and Latest Premium Content on the bottom. I am hoping the answer will be something simple like:

Code: Select all
<?php $args = array(
    
'posts_per_page' => 1,
    
'post__in'  => get_option's2member_posts' ),
    
'ignore_sticky_posts' => 1
);
query_posts$args );?>
      <?php rewind_posts(); ?>
      <?php while (have_posts()) : the_post(); ?>


Alas, I expect it will be something a bit more challenging!
Thank you in advance for your help and I hope I haven't rambled too much!
User avatar
Terry
Registered User
Registered User
 
Posts: 3
Joined: July 26, 2011

Re: Members only latest posts page

Postby Cristián Lávaque » July 27th, 2011, 12:34 am

Thanks for the kudos! I'm very glad you're liking s2Member. :)

You can check what content is protected looking at these:

Code: Select all
$GLOBALS['WS_PLUGIN__']['s2member']['o'][level0_ruris]
$GLOBALS['WS_PLUGIN__']['s2member']['o'][level1_ruris]
$GLOBALS['WS_PLUGIN__']['s2member']['o'][level2_ruris]
$GLOBALS['WS_PLUGIN__']['s2member']['o'][level3_ruris]
$GLOBALS['WS_PLUGIN__']['s2member']['o'][level4_ruris]
$GLOBALS['WS_PLUGIN__']['s2member']['o'][level0_catgs]
$GLOBALS['WS_PLUGIN__']['s2member']['o'][level1_catgs]
$GLOBALS['WS_PLUGIN__']['s2member']['o'][level2_catgs]
$GLOBALS['WS_PLUGIN__']['s2member']['o'][level3_catgs]
$GLOBALS['WS_PLUGIN__']['s2member']['o'][level4_catgs]
$GLOBALS['WS_PLUGIN__']['s2member']['o'][level0_ptags]
$GLOBALS['WS_PLUGIN__']['s2member']['o'][level1_ptags]
$GLOBALS['WS_PLUGIN__']['s2member']['o'][level2_ptags]
$GLOBALS['WS_PLUGIN__']['s2member']['o'][level3_ptags]
$GLOBALS['WS_PLUGIN__']['s2member']['o'][level4_ptags]
$GLOBALS['WS_PLUGIN__']['s2member']['o'][level0_posts]
$GLOBALS['WS_PLUGIN__']['s2member']['o'][level1_posts]
$GLOBALS['WS_PLUGIN__']['s2member']['o'][level2_posts]
$GLOBALS['WS_PLUGIN__']['s2member']['o'][level3_posts]
$GLOBALS['WS_PLUGIN__']['s2member']['o'][level4_posts]
$GLOBALS['WS_PLUGIN__']['s2member']['o'][level0_pages]
$GLOBALS['WS_PLUGIN__']['s2member']['o'][level1_pages]
$GLOBALS['WS_PLUGIN__']['s2member']['o'][level2_pages]
$GLOBALS['WS_PLUGIN__']['s2member']['o'][level3_pages]
$GLOBALS['WS_PLUGIN__']['s2member']['o'][level4_pages] 


I hope it helps.
Cristián Lávaque http://s2member.net
Is s2Member working for you? Please rate it Image at WordPress.org. Thanks! :)
User avatar
Cristián Lávaque
Developer
Developer
 
Posts: 6836
Joined: December 22, 2010

Re: Members only latest posts page

Postby Terry » August 3rd, 2011, 10:01 am

Hi Cristián,
Thanks for the reply, that was exactly what I needed!

It's taken a bit of work but I have managed to get it working, below is a solution if anyone has the same problem when building a them:

Code: Select all

<?php 
        $sticky_top 
= get_option( 'sticky_posts' );
        $res_1 = $GLOBALS['WS_PLUGIN__']['s2member']['o'][level0_posts];
        $OutputArray2 = explode(",", $res_1);
        rsort( $OutputArray2 );
        rsort($sticky_top);
        $ArrayCount = 0;
        foreach ($OutputArray2 as $OutputArray2ArrayElement){
            if($OutputArray2ArrayElement <> ""){
                foreach ($sticky_top as $sticky_topArrayElement){
                    if($sticky_topArrayElement <> "" && $OutputArray2ArrayElement == $sticky_topArrayElement){
                        $MemberSticky[$ArrayCount] = $OutputArray2ArrayElement;
                        $CurrentSticky = $OutputArray2ArrayElement;
                        $ArrayCount ++;
                        break 2;
                    }
                };
            }
        };
        
        $args 
= array(
        'posts_per_page' => 1,
        'post__in'  => $MemberSticky,
        'ignore_sticky_posts' => 1
        
);

        query_posts( $args );?>
         <?php rewind_posts(); ?>
        
          <?php while (have_posts()) : the_post(); ?>
      <h1><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
        <?php the_title(); ?>
        </a></h1>
      <br />
      <?php 
        if 
( has_post_thumbnail() ) {
            echo '<div class="homepost_thumbnail">';
            the_post_thumbnail();
            echo '</div>';
        } 
        the_excerpt
('Read more &raquo;'); ?>
     <?php endwhile; ?>


In english, this code is getting all sticky posts using the variable
Code: Select all
$sticky_top = get_option( 'sticky_posts' );


Using the Globals from Cristián, I have selected only level 0 posts to be shown (as I will have different dashboards for each member level if an when we chose to implement it. Therefore the variable here is
Code: Select all
$res_1 = $GLOBALS['WS_PLUGIN__']['s2member']['o'][level0_posts];


Code: Select all
$OutputArray2 = explode(",", $res_1);
"explodes" the $Globals into an array.

In order to get the chronological order of the posts I have used:
Code: Select all
rsort( $OutputArray2 );
rsort($sticky_top);


Now for the actual work:
I create a counter
Code: Select all
$ArrayCount = 0;
, this is so the array produced by the following code will start its array at 0.

In order to decide which one of the values in $res_1 (now $OutputArray2) is the latest sticky post we need to separate each individual variable from the array and see if it is equal to each individual element of the $sticky_top array.
The first part of this is to separate $OutputArray2:

Code: Select all
foreach ($OutputArray2 as $OutputArray2ArrayElement) {


We need to check if $OutputArray2ArrayElement has a value
Code: Select all
if($OutputArray2ArrayElement <> ""){
if so then separate out the sticky post array ($sticky_top):

Code: Select all
foreach ($sticky_top as $sticky_topArrayElement){


Again, check if there is actually an object in the array element "$sticky_topArrayElement" with the if statement:
Code: Select all
if($sticky_topArrayElement <> ""
And now that our array elements are separated out, see if the one element is equal to another:
Code: Select all
&& $OutputArray2ArrayElement == $sticky_topArrayElement){


This will loop through the foreach statement and output every element that is equal to the top element, each time it finds and element it outputs it and adds 1 to the $ArrayCount.
This information is currently useless as it is not in a variable (specifically an array object as WordPress will not accept anything other than an array for 'post__in').

So, the above is done by putting it into a variable:
Code: Select all
$MemberSticky[$ArrayCount] = $OutputArray2ArrayElement;
and adding to the $ArrayCount
Code: Select all
$ArrayCount ++;


We then close the foreach loops with
Code: Select all
break 2;
so that we don't leave it continuously running! All lose ends are closed off with a "}" and we move on to WordPress's Loop:

All of the arguments needed for The Loop are stored in the array "$args" as so:
Code: Select all
$args = array(
        'posts_per_page' => 1,
        'post__in'  => $MemberSticky,
        'ignore_sticky_posts' => 1
);


$MemberSticky is the result of our efforts in the foreach loop. Wordpress is going to only show one post, from the $MemberSticky array and check if it's sticky (technically we don't need this as we've already decided it's sticky!).

Next run the query
Code: Select all
query_posts( $args );?>
and do the standard
Code: Select all
<?php while (have_posts()) : the_post(); ?>


All the stuff in between goes here (i.e. title, the content etc.) then close the while
Code: Select all
<?php endwhile; ?>


You will notice in the big block of code at the top, two items "$CurrentSticky = $OutputArray2ArrayElement;" and
Code: Select all
<?php rewind_posts(); ?>
. The reason for the "rewind_posts" is because I want to do another loop on the page, and the $CurrentSticky variable is used in my next loop.
The next loop displays all s2Member level 0 posts, excluding the sticky post above. I won't go into more detail, but the whole template page is attached
dashboard-page.txt
change extension to php and upload to your theme folder. Will require editing to fit into your theme! When installed, go to wordpress, add a new page and select the template "User Dashboard Page"
(4.6 KiB) Downloaded 21 times


Hope this saves people time and they find it useful! Thanks again Cristián

Terry
User avatar
Terry
Registered User
Registered User
 
Posts: 3
Joined: July 26, 2011

Re: Members only latest posts page

Postby Cristián Lávaque » August 3rd, 2011, 10:55 pm

Awesome, Terry! I'm very glad it helped you solve it and thanks a lot for sharing your solution! :)
Cristián Lávaque http://s2member.net
Is s2Member working for you? Please rate it Image at WordPress.org. Thanks! :)
User avatar
Cristián Lávaque
Developer
Developer
 
Posts: 6836
Joined: December 22, 2010

Re: Members only latest posts page

Postby Terry » September 16th, 2011, 7:40 am

No problem, hope it's useful!
User avatar
Terry
Registered User
Registered User
 
Posts: 3
Joined: July 26, 2011


Return to s2Member Plugin

Who is online

Users browsing this forum: Exabot [Bot], Google [Bot] and 1 guest

cron