-1

This issue is now finally resolved by myself. Find the solution in the end of this post. Question was in short: How can i ask in my WP template, IF the whole array of have_posts contains any post from a certain category

I programmed my tag page so that the output is seperated into three styled boxes according to the categories. The loop is done three times and with an IF i sort out the categories. This is a simplified! draft of the working structure:

<div><ul>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
  <?php if (in_category('cat1')) : ?>
  <li>...
<endif>
</ul></div>
<div><ul>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
  <?php if (in_category('cat2')) : ?>
  <li>...
<endif>
</ul></div>
<div><ul>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
  <?php if (in_category('cat3')) : ?>
  <li>...
<endif>
</ul></div>

Here you can see the output: http://imlichte.net/tag/interdependenz/

Now my point is to avoid emtpy boxes when no posts are found in a category. The boxes (divs) should then not be rendered at all. For this i thought to put an IF condition for the div and ul, which are outside of the loop. So i want to actually ask this thing:

IF have_posts contains posts from a certain category

The div and ul that creates the box on the page will only be shown if the result is not zero, thus the result of suppressing empty boxes is reached.

I hope i could explain my request well enough to be understood and hope for a solution from the experts, since i am just a hobby programmer.

Thanks for help Sofian

Edit: to make it very clear, here comes the whole code that generates my category structured tag list:

<div class="box">
  <h2 class="archive-title" style="text-align:center">Artikel</h2>
  <hr />
    <ul class="postlist">       

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
  <?php if (in_category('wissen')) : ?>

    <li>

    <?php if (has_post_thumbnail()): ?>
        <a href="<?php the_permalink();?>"><?php the_post_thumbnail(array(150,150));?>
    <?php else: ?>
        <div class="postlistfilling"></div>
    <?php endif; ?>      

    <a class="postlisttitle" href="<?php the_permalink();?>"><?php the_title(); ?></a>

        <div class="pthumb">  
        <?php the_excerpt();?>
        </div>

        </li>
    <?php endif; endwhile; endif; ?>
    </ul>
  </div>

  <div class="box">
  <h2 class="archive-title" style="text-align:center">Empfehlungen</h2>
  <hr />
  <ul class="postlist">     

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
  <?php if (in_category('empfehlungen')) : ?>

    <li>

    <?php if (has_post_thumbnail()): ?>
        <a href="<?php the_permalink();?>"><?php the_post_thumbnail(array(150,150));?>
    <?php else: ?>
        <div class="postlistfilling"></div>
    <?php endif; ?>      

    <a class="postlisttitle" href="<?php the_permalink();?>"><?php the_title(); ?></a>

        <div class="pthumb">  
        <?php the_excerpt();?>
        </div>

        </li>
    <?php endif; endwhile; endif; ?>
    </ul>
  </div>

  <div class="box">
  <h2 class="archive-title" style="text-align:center">Seminare</h2>
  <hr />
  <ul class="postlist">     

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
  <?php if (in_category('seminare')) : ?>

    <li>

    <?php if (has_post_thumbnail()): ?>
        <a href="<?php the_permalink();?>"><?php the_post_thumbnail(array(150,150));?>
    <?php else: ?>
        <div class="postlistfilling"></div>
    <?php endif; ?>      

    <a class="postlisttitle" href="<?php the_permalink();?>"><?php the_title(); ?></a>

        <div class="pthumb">  
        <?php the_excerpt();?>
        </div>

        </li>
    <?php endif; endwhile; endif; ?>
    </ul>
  </div>

The solution: I run through the whole loop once just to set a variable to true if it comes across a post in that category. If there is no post, the var stays false and the Div Box around the actual loop will not be output.

<?php $categoryexisting = false; 
  if (have_posts()) : while (have_posts()) : the_post();
  if (in_category('articles')) :
    $categoryexisting = true;             
  endif; endwhile; endif; ?>

  <?php if ($categoryexisting): ?>   
  <div class="box">

  <h2 class="archive-title" style="text-align:center">Articles</h2>
  <hr />
    <ul class="postlist">   
  <?php endif; ?>

... Actual loop ...

  • 1
    It's <?php endif ?>, not <endif>. It's PHP, not an HTML element. – Aibrean Jul 18 '14 at 19:35
  • This is just meant as a simplified draft. It is working online so the real code is correct. It is too long to post completely – Sofian Jul 18 '14 at 19:47
  • Please don't shout, you are hurting my eyes – Pieter Goosen Aug 1 '14 at 17:02
1

Per your example, the contents have to be contained in the if/while statement (if they have posts). Otherwise the boxes will appear and the content will be blank, rather than the content AND boxes hidden completely as you want.

Try this:

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
        <div class="box">
            <h2 class="archive-title" style="text-align:center">Artikel</h2>
            <hr />
            <ul class="postlist"> 
                <?php if (in_category('wissen')) : ?>

                    <li>

                        <?php if (has_post_thumbnail()): ?>
                            <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(array(150, 150)); ?>
                            <?php else: ?>
                                <div class="postlistfilling"></div>
                            <?php endif; ?>      

                            <a class="postlisttitle" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>

                            <div class="pthumb">  
                                <?php the_excerpt(); ?>
                            </div>

                    </li>

                </ul>
            </div>  
        <?php endif; endwhile; endif; ?>
  • I knew about this possibility, but this structure doesnt help me to output my boxes. I need what i described above. To show it better, here you can see the output: imlichte.net/tag/interdependenz – Sofian Jul 18 '14 at 19:51
  • @Sofian I just adjusted my answer. I'm still not entirely clear on what you are trying to do and your code example is as you said, not an accurate representation of what you have. – Aibrean Jul 18 '14 at 19:55
  • 1
    Here comes the whole code. If you have an idea how to output that what i have there, with the structure you suggested, it is also fine. for the moment i dont think it does what i need. i cant post the code here, i will edit my original post – Sofian Jul 18 '14 at 20:18
  • Ok...Your code is fine. The issue is your div and ul should be WITHIN the if/while statement for posts. I will update my answer. – Aibrean Jul 18 '14 at 20:23
  • Oh my God, yes, this looks like the solution. A case of "this was too simple" or "not seeing the forest because of too many trees". I cant try it out on the spot but tomorrow i will give it a shot. But logically this is it, for me these two IF lines were glued together in my mind. Thank you very much... – Sofian Jul 18 '14 at 21:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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