• Resolved check12

    (@check12)


    If I’m in a category I want to start a <ul> and then the Loop. In the Loop I output some <li>‘s and the_excerpt(). Right after that I close the Loop.

    Else: If I’m not in a category output the_content().

    <?php if ( is_category() ) { ?>
    	<ul>
    		<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    		<li>Text in li that will be repeated @the loop</li>
                    <?php the_excerpt(); ?>
    		<?php endwhile; else: ?>
    		<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    		<?php endif; ?>
    	</ul>
    
    <?php } else {
    	the_content();
    } ?>

    What worx: When I’m on a category everything is fine, the Text and the_excerpt() are shown But when I click on a post, there is no content! Only the Header and Footer are shown but the_content() disappears. Is my else statement not correct?

    Can anybody help me to make this code work?
    Thank you!

Viewing 6 replies - 1 through 6 (of 6 total)
  • instead of just ‘the_content()’ you need to put the whole loop, obviously with ‘the_content()’ instead ‘the_exerpt()’.

    <?php
    // If there are posts
    if (have_posts()) :
    ?>
    	<ul>
    
    	<?php // While there are posts, loop over this code
    	while (have_posts()) : the_post();
    	?>
    	<li>Text in li that will be repeated @the loop
    
    	<?php if ( is_category() ) : ?>
    		<?php the_excerpt(); ?>
    	<?php else : ?>
    		<?php the_content(); ?>
    	<?php endif; ?>
    
    	</li>
    	<?php // End while loop
    	endwhile;
    	?>
    
    	</ul>
    <?php // Else if no posts
    else:
    ?>
    
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    
    <?php // End if posts
    endif;
    ?>
    Thread Starter check12

    (@check12)

    Thanks so much, I test it!

    EDIT: Thanks but that’s not what I wanted. The <ul> has to be out of the loop and I need an if statement before the loop starts if I’m in a category. I try to use your code and rebuild it.

    Thread Starter check12

    (@check12)

    I tried to rebuild it, but I’m still failing. Same issue, I can’t get the_content() to be outputted.

    You can’t use the_content() outside of a loop, it’s essentially part of the loop, using it outside of that context requires a custom query..

    If you could explain precisely what you’re trying to archieve then i might be able to suggest something else..

    Thread Starter check12

    (@check12)

    you are right, I solved the problem bei adding if statements within the loop. that works, thanks

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘php code in the loop’ is closed to new replies.