Title: Conditional Posts Loop based on Current User Can
Last modified: August 21, 2016

---

# Conditional Posts Loop based on Current User Can

 *  [craigtommola](https://wordpress.org/support/users/craigtommola/)
 * (@craigtommola)
 * [12 years ago](https://wordpress.org/support/topic/conditional-posts-loop-based-on-current-user-can/)
 * I have the idea of what I’m trying to accomplish, and I believe that it’s feasible,
   but my lack of experience with PHP syntax is mucking it up. Here’s the basics
   of it, without a lot of backstory.
 * I would like to modify this part of the loop:
    `<?php if ( have_posts() ) : ?
   >`
 * To run conditionally if `current_user_can('manage_links')`
 * I tried a handful of attempts at _if have posts AND user can manage links_, but
   again I believe my syntax is incorrect.
 * If the user cannot manage links, I would like the loop to return the default 
   message, which in my case is modified to be “You do not have permission to view
   this content.”
 * Please lend your thoughts.
 * Thank you kindly
    CT

Viewing 8 replies - 1 through 8 (of 8 total)

 *  [wplovin](https://wordpress.org/support/users/wplovin/)
 * (@wplovin)
 * [12 years ago](https://wordpress.org/support/topic/conditional-posts-loop-based-on-current-user-can/#post-4839949)
 *     ```
       <?php if( have_posts() && current_user_can('manage_links') ) : ?>
       //happens if query have posts AND current_user_can('manage_links') returns TRUE
       <?php elseif( have_posts() && !current_user_can('manage_links') ) : ?>
       //happens if query have posts AND current_user_can('manage_links') returns FALSE
       <?php else : ?>
       //happens if query don't have any posts
       <?php endif; ?>
       ```
   
 * Something like this, maybe?
 *  [wplovin](https://wordpress.org/support/users/wplovin/)
 * (@wplovin)
 * [12 years ago](https://wordpress.org/support/topic/conditional-posts-loop-based-on-current-user-can/#post-4839953)
 * Btw, you might also want to add [is_user_logged_in](http://codex.wordpress.org/Function_Reference/is_user_logged_in)
   check, but that depends on how you plan implementing this.
 * Tom
 *  Thread Starter [craigtommola](https://wordpress.org/support/users/craigtommola/)
 * (@craigtommola)
 * [12 years ago](https://wordpress.org/support/topic/conditional-posts-loop-based-on-current-user-can/#post-4839957)
 * Thank you! I will have to review and test this out. One question – if `current_user_can``
   manage_links`, wouldn’t they **have to** be logged in?
 *  [wplovin](https://wordpress.org/support/users/wplovin/)
 * (@wplovin)
 * [12 years ago](https://wordpress.org/support/topic/conditional-posts-loop-based-on-current-user-can/#post-4839962)
 * Yup, if user is not logged in, current_user_can() function will always return
   FALSE.
 * Tom
 *  Thread Starter [craigtommola](https://wordpress.org/support/users/craigtommola/)
 * (@craigtommola)
 * [12 years ago](https://wordpress.org/support/topic/conditional-posts-loop-based-on-current-user-can/#post-4839963)
 * False in this case would return “You do not have permission to view this content.”…
   correct?
 * Thanks again – so quick and so helpful.
 * CT
 *  Thread Starter [craigtommola](https://wordpress.org/support/users/craigtommola/)
 * (@craigtommola)
 * [12 years ago](https://wordpress.org/support/topic/conditional-posts-loop-based-on-current-user-can/#post-4839967)
 * Below is the order of the loop I’m working with. It’s the index.php from twentytwelve,
   which I’ve moved into a child theme.
 * I can’t seem to get the arguments in the right order – sometimes getting the “
   no posts” message and others getting the white screen of death.
 * Would you mind terribly dropping your above arguments in place?
 *     ```
       <?php if ( have_posts() ) : ?>
   
       			<?php /* Start the Loop */ ?>
       			<?php while ( have_posts() ) : the_post(); ?>
       				<?php get_template_part( 'content', get_post_format() ); ?>
       			<?php endwhile; ?>
   
       			<?php twentytwelve_content_nav( 'nav-below' ); ?>
   
       		<?php else : ?>
   
       			<article id="post-0" class="post no-results not-found">
   
       			<?php if ( current_user_can( 'edit_posts' ) ) :
       				// Show a different message to a logged-in user who can add posts.
       			?>
       				<header class="entry-header">
       					<h1 class="entry-title"><?php _e( 'There are no listings available.', 'twentytwelve' ); ?></h1>
       				</header>
   
       				<div class="entry-content">
       					<p><?php print( __( 'If you would like to submit a listing to the marketplace, please fill out <a href="/submit/" title="Submit">this simple form</a>.', 'twentytwelve' ) ); ?></p>
       				</div><!-- .entry-content -->
   
       			<?php else :
       				// Show the default message to everyone else.
       			?>
       				<header class="entry-header">
       					<h1 class="entry-title"><?php _e( 'Sorry', 'twentytwelve' ); ?></h1>
       				</header>
   
       				<div class="entry-content">
       					<p><?php _e( 'You do not have permission to view this content.', 'twentytwelve' ); ?></p>
       				</div><!-- .entry-content -->
       			<?php endif; // end current_user_can() check ?>
   
       			</article><!-- #post-0 -->
   
       		<?php endif; // end have_posts() check ?>
       ```
   
 *  [wplovin](https://wordpress.org/support/users/wplovin/)
 * (@wplovin)
 * [12 years ago](https://wordpress.org/support/topic/conditional-posts-loop-based-on-current-user-can/#post-4839970)
 * Try replacing it with this one:
 *     ```
       <?php if( have_posts() && current_user_can('manage_links') ) : ?>
   
       	<?php while ( have_posts() ) : the_post(); ?>
       		<?php get_template_part( 'content', get_post_format() ); ?>
       	<?php endwhile; ?>
       	<?php twentytwelve_content_nav( 'nav-below' ); ?>
   
       <?php elseif( have_posts() && !current_user_can('manage_links') ) : ?>
   
       	<article id="post-0" class="post no-results not-found">
   
       		<header class="entry-header">
       			<h1 class="entry-title"><?php _e( 'You do not have permission to view this content.', 'twentytwelve' ); ?></h1>
       		</header>
       		<div class="entry-content">
       			<p><?php _e('You do not have permission to view this content.', 'twentytwelve' ) ); ?></p>
       		</div>
   
       	</article>
   
       <?php else : ?>
   
       	<article id="post-0" class="post no-results not-found">
   
       	<?php if ( current_user_can( 'edit_posts' ) ) : ?>
       		<header class="entry-header">
       			<h1 class="entry-title"><?php _e( 'There are no listings available.', 'twentytwelve' ); ?></h1>
       		</header>
       		<div class="entry-content">
       			<p><?php print( __( 'If you would like to submit a listing to the marketplace, please fill out <a href="http://36webhosting.com/wwp/members/marketplace/submit/" title="Submit">this simple form</a>.', 'twentytwelve' ) ); ?></p>
       		</div>
       	<?php else : ?>
       		<header class="entry-header">
       			<h1 class="entry-title"><?php _e( 'Sorry', 'twentytwelve' ); ?></h1>
       		</header>
       		<div class="entry-content">
       			<p><?php _e( 'You do not have permission to view this content.', 'twentytwelve' ); ?></p>
       		</div>
       	<?php endif; ?>
   
       	</article>
   
       <?php endif; ?>
       ```
   
 * Note: haven’t tested this, but it should work, haha 😀
 *  Thread Starter [craigtommola](https://wordpress.org/support/users/craigtommola/)
 * (@craigtommola)
 * [12 years ago](https://wordpress.org/support/topic/conditional-posts-loop-based-on-current-user-can/#post-4840113)
 * It’s not working out for me. The No Posts message shows to the user who can manage
   links – even when there are posts. The No Permission message shows correctly 
   to a visitor.
 * Thoughts?
    Thx CT

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘Conditional Posts Loop based on Current User Can’ is closed to new replies.

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 8 replies
 * 2 participants
 * Last reply from: [craigtommola](https://wordpress.org/support/users/craigtommola/)
 * Last activity: [12 years ago](https://wordpress.org/support/topic/conditional-posts-loop-based-on-current-user-can/#post-4840113)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
