$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - Add more button if more 8 items|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Add more button if more 8 items

matteradmin9PV0评论

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
Add a comment  | 

1 Answer 1

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.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far