logo

$10
If there are X number of posts

Hi guys,
I'm looking for a conditional statement that will hide/show a button based on the number of posts. If you check out the attached image, you'll see I'm showing 6 portfolio items at a time. If there are more than 6, you can click the More button (top right) and flip through more items (on the same page).

The problem is, if there are less than 6 items, the button still shows. I would like to count the number of posts and hide the button if there are less than 6 items. I should also note that currently the button is outside the loop, but if needed I may be able to jam it in there.

You can also check out my code here:



<div class="slide-buttons-right">
<ul>
<?php wp_list_categories('title_li=<b>Categories: </b>&child_of='.of_get_option('of_portfolio_cat', 'no entry' )); ?></ul>
<a class="next next-button" href="#">More Projects &rarr;</a>
</div>

<div class="features-portfolio">
<!-- loop for portfolio slider -->
<?php query_posts('showposts=45&cat='.of_get_option('of_portfolio_cat', 'no entry' )); ?>

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

<ul>
<li class="group group-portfolio">
<div class="feature">
<div class="feature-shot">
<div class="feature-img">
<a href="
<?php if ( get_post_meta($post->ID, 'boxlink', true) ) { ?>
<?php echo get_post_meta($post->ID, 'boxlink', true) ?>
<?php } else { ?>
<?php the_permalink(); ?>
<?php } ?>
" class="feature-link"><?php the_post_thumbnail( 'feature' ); ?></a>
<a href="<?php the_permalink(); ?>" class="feature-over">
<?php the_title(); ?>
<span class="dim"><?php echo get_the_date(); ?></span>
<?php the_author_link(); ?>
</a>
</div>
</div>
</div>
</li>
</ul>

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

attachment image View Attachment

Mike McAlister | 06/14/11 at 2:50pm | Edit


(4) Possible Answers Submitted...

  • avatar
    Last edited:
    06/14/11
    2:56pm
    Utkarsh Kukreti says:

    <?php
    global $wp_query;
    if ($wp_query->found_posts > 6) { ?>
    <a class="next next-button" href="#">More Projects &rarr;</a>
    <?php } ?>

  • avatar
    Last edited:
    06/14/11
    3:00pm
    Kailey Lampert says:

    replace your current line:

    <a class="next next-button" href="#">More Projects &rarr;</a>

    with this

    <?php
    global $wp_query;
    $total = $wp_query->found_posts;
    if ($total > 6) {
    ?><a class="next next-button" href="#">More Projects &rarr;</a><?php
    }
    ?>

    Edit: found_posts would be correct :)

    Previous versions of this answer: 06/14/11 at 3:00pm

    • 06/14/11 7:00pm

      Mike McAlister says:

      Hmm, that one doesn't seem to work, unfortunately. Just made the button disappear.

      Mike

    • 06/14/11 7:17pm

      Kailey Lampert says:

      Can you echo out $total and see what it says?


      <?php
      global $wp_query;
      $total = $wp_query->found_posts;
      echo $total;
      if ($total > 6) {
      ?><a class="next next-button" href="#">More Projects &rarr;</a><?php
      }
      ?>

    • 06/14/11 8:07pm

      Kailey Lampert says:

      You might also try moving the query_posts() function up a few lines so that it comes before your "more projects" button

  • avatar
    Last edited:
    06/14/11
    10:36pm
    Jerson Baguio says:

    Try this code it might fix your problem



    <div class="slide-buttons-right">

    <ul>

    <?php wp_list_categories('title_li=<b>Categories: </b>&child_of='.of_get_option('of_portfolio_cat', 'no entry' )); ?></ul>
    <?php
    global $wp_query;
    $wp_query = new WP_Query('showposts=45&cat='.of_get_option('of_portfolio_cat', 'no entry' ));
    ?>
    <?php

    if($wp_query->post_count > 6):
    ?>
    <a class="next next-button" href="#">More Projects &rarr;</a>
    <?php endif;?>
    </div>



    <div class="features-portfolio">

    <!-- loop for portfolio slider -->

    <?php if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>



    <ul>

    <li class="group group-portfolio">

    <div class="feature">

    <div class="feature-shot">

    <div class="feature-img">

    <a href="

    <?php if ( $wp_query->get_post_meta($post->ID, 'boxlink', true) ) { ?>

    <?php echo $wp_query->get_post_meta($post->ID, 'boxlink', true) ?>

    <?php } else { ?>

    <?php $wp_query->the_permalink(); ?>

    <?php } ?>

    " class="feature-link"><?php $wp_query->the_post_thumbnail( 'feature' ); ?></a>

    <a href="<?php $wp_query->the_permalink(); ?>" class="feature-over">

    <?php $wp_query->the_title(); ?>

    <span class="dim"><?php echo $wp_query->get_the_date(); ?></span>

    <?php $wp_query->the_author_link(); ?>

    </a>

    </div>

    </div>

    </div>

    </li>

    </ul>



    <?php endwhile; ?>

    <?php endif; ?>

    </div>



  • avatar
    Last edited:
    06/15/11
    2:09pm
    Just Me says:

    why don't you add it to the code

    query_posts( 'posts_per_page=5' );

This question has expired.



Mike McAlister voted on this question.



Current status of this question: Completed