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>
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>
Share
Improve this question
edited Jan 23, 2019 at 20:26
Krzysiek Dróżdż
25.6k9 gold badges53 silver badges74 bronze badges
asked Jan 23, 2019 at 20:24
Yavar MammadovYavar Mammadov
132 bronze badges
3
- Should it display all countries or only first 8 and then more button? – Krzysiek Dróżdż 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 – Yavar Mammadov Commented Jan 23, 2019 at 20:28
- <?php if($countries > 1) { echo '<span class="under-menu__title">TEST</span>'; } ?> – Yavar Mammadov Commented Jan 23, 2019 at 20:28
1 Answer
Reset to default 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.