0

Im trying to achieve the code hides part of the content from unregistered users but doesn't work for me and comes back with error this is the code i tried

if ( is_user_logged_in() ) {
echo '<div class="container_12">
<?php while ( have_posts() ) : the_post(); ?>
<?php woocommerce_get_template_part( 'content', 'single-product' ); ?>
<?php endwhile; // end of the loop. ?>
</div>}
else {
echo 'you must to log in to see the content';}
?>
1
  • 2
    You seriously have a lot of syntax errors. Feb 25, 2015 at 18:04

1 Answer 1

1

This is actually a PHP syntax error, not a WordPress error. When you issue an echo command and start a text string (using either single or double quotes), PHP will read everything that follows as part of that string until you close it with the same quote character.

So in your case, you open the text string with <div class="contain... but don't close the string at the end of the line with a closing quote -- so as far as PHP is concerned, you're still sending a string for it to echo. That means the whole code block is thrown off (each subsequent single quote is seen as the opposite of its intended purpose, i.e. an opening quote is read as a closing quote, and vice versa), and the closing curly bracket (}) for this if statement is treated as part of a text string -- which means it's not seen by PHP as the end of this section of your if statement.

(The other issue you have here is that you're opening PHP tags using <? & ?> when you're already inside of a block of PHP. In this case, those tag brackets can be removed.)

All of that said, you're probably trying to do something like this:

if ( is_user_logged_in() ) {
 echo '<div class="container_12">';
 while ( have_posts() ) : the_post();
  woocommerce_get_template_part( 'content', 'single-product' );
 endwhile; // end of the loop.
 echo '</div>';
}
else {
 echo 'you must to log in to see the content';
}
2
  • thank you, i copied the code and pasted it works, but it seems that it hides more than it should be, i need only to hide the part of woocommerce_get_template_part( 'content', 'single-product' );
    – Wordpressy
    Feb 25, 2015 at 18:13
  • In that case, you probably want to move the two echo '...'; statements outside of the if/else code block. Feb 25, 2015 at 18:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.