1

I want to add a "more" button if more than 8 elements, I tried it

        <div class="under-menu__countries">
            <span class="under-menu__title">Countries</span>
            <?php
                $args = array(
                    'posts_per_page' => -1,
                    'post_type' => 'post',
                    'cat' => $li->term_id,
                    'orderby' => 'date',
                    'order' => 'DESC',
                );
                // var_dump(  $args);
                $countries = new WP_Query($args);
                if ( $countries->have_posts() ) : while ( $countries->have_posts() ) : $countries->the_post(); 
                $country_flag = get_post_meta($post->ID, 'country', true);
                $country_flag = explode(" ",$country_flag);
                $country_flag = implode("-",$country_flag);
                ?>
                <a href="<?php the_permalink(); ?>" class="um__counties--wrap">
                    <img src="<?php echo get_template_directory_uri(); ?>/pictures/flags/24/<?php echo $country_flag; ?>.png"  alt="<?php the_title(); ?>"
                        title="<?php the_title(); ?> photo"
                        class=""/>
                        <span><?php the_title(); ?></span>                            
                </a>
                <?php 
                    if($countries > 7) {
                        echo "more";
                    }
                 ?>
            <?php endwhile; endif; wp_reset_query(); ?>
        </div>
3
  • Should it display all countries or only first 8 and then more button? Commented Jan 23, 2019 at 20:25
  • @KrzysiekDróżdż I have all the countries displayed, but I hid the styles, but I want the “more” button to be added after 8 and I could style it Commented Jan 23, 2019 at 20:28
  • <?php if($countries > 1) { echo '<span class="under-menu__title">TEST</span>'; } ?> Commented Jan 23, 2019 at 20:28

1 Answer 1

0

$countries is not a number, but w WP_Query instance, so you can't compare it with a number...

This means that you should replace this:

if ($countries > 7) {

with:

if ( $countries->found_posts > 7 ) {

It will check if there are more than 8 countries that match your criteria. This way you can even change '-1' to '8' and limit the number of posts you're displaying and this condition will still work.

1
  • Thanks, checked out of the loop and everything works, but if -1 to put does not work. put 8 everything works Commented Jan 23, 2019 at 20:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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