• 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)
  • <?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?

    Btw, you might also want to add is_user_logged_in check, but that depends on how you plan implementing this.

    Tom

    Thread Starter craigtommola

    (@craigtommola)

    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?

    Yup, if user is not logged in, current_user_can() function will always return FALSE.

    Tom

    Thread Starter craigtommola

    (@craigtommola)

    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

    (@craigtommola)

    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 ?>

    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="https://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

    (@craigtommola)

    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.