简体   繁体   中英

Wordpress while loop: Show all posts instead of just 10

I'm really breaking my neck over this one... This is a widget in wordpress that show that last "Post" in this case "Sections".

The only problem I have that it shows only the last 10 items... But I need to show them all.

Is there anyone that can see why this is the case?

I already set all the values from 3 to 10 but that didn't solve my issue and i really don't know what to do anymore:(

Regards, Robert

public function widget( $args, $instance ) {
            if ( ! isset( $args['widget_id'] ) ) {
                $args['widget_id'] = $this->id;
            }

            $title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : __( 'Recent Posts' );

            /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
            $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );

            $number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 50;
            if ( ! $number )
                $number = 50;
            $show_date = isset( $instance['show_date'] ) ? $instance['show_date'] : false;

            /**
             * Filters the arguments for the Recent Posts widget.
             *
             * @since 3.4.0
             *
             * @see WP_Query::get_posts()
             *
             * @param array $args An array of arguments used to retrieve the recent posts.
             */

             $args = array( 'post_type' => 'section' );
             $r = new WP_Query( $args );

            /* $r = new WP_Query( apply_filters( 'widget_posts_args', array(
                'posts_per_page'      => $number,
                'no_found_rows'       => true,
                'post_status'         => 'publish',
                'ignore_sticky_posts' => true
            ) ) ); */



            ?>
            <?php echo $args['before_widget']; ?>

            <div id="product_home_id" class="product_home">
                <div class="page-homepage">
                    <ul>
                        <?php while ( $r->have_posts() ) : $r->the_post(); ?>
                            <li class="menu-item">
                                <?php /* <a class="anchor" href="<?php echo home_url(); ?>/#section-<?php the_ID(); ?>"><?php echo get_post_meta($post->ID, "url", true); ?><?php the_post_thumbnail(); ?><br/><?php the_title(); ?></a> */ ?>
                                <a class="anchor" href="<?php echo get_permalink(); ?>"><?php echo get_post_meta($post->ID, "url", true); ?><?php the_post_thumbnail(); ?><br/>
                                    <div class="product_home_name_des">
                                        <div class="product_description_home">
                                            <?php the_field('product_description'); ?>
                                        </div>
                                        <span class="product_name">
                                            <?php the_title(); ?>
                                        </span>
                                    </div>
                                </a>
                                <div class="product_content">
                                    <a href="<?php echo get_permalink(); ?>">
                                        <div class="arrow"><img src="<?php echo get_stylesheet_directory_uri(); ?>/images/arrow_image.png" /></div>
                                        <p class="content"><?php the_field('product_subtitle'); ?></p>
                                    </a>
                                </div>
                            </li>
                        <?php endwhile; ?>
                    </ul>
                </div>
            </div>

            <?php echo $args['after_widget']; ?>
            <?php
            // Reset the global $the_post as this query will have stomped on it
            wp_reset_postdata();
        }

Just add this to your $args:

'posts_per_page' => -1

You can also change the default setting in admin to something other than 10. Go to the WordPress admin > Settings > Reading. There is an option for "Blog pages show at most".

Another approach might be to use

'nopaging' => true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM