Pagination zeigt nicht alle Beiträge auf Index.php

[ad_1]

Der Entscheidende Hinweis zur Lösung meines Problems kam vom Moderator des englischen WP Forum: pre_get_posts war mir noch nicht bekannt.

Die Lösung dann mit dieser Funktion:

function numberposts_for_index( $query ) {
if( $query->is_main_query() && ! is_admin() && $query->is_home() ) {
$query->set( 'posts_per_page', '4' );
$query->set('post_status','future,publish');
}}
add_action( 'pre_get_posts', 'numberposts_for_index' );

und auf der Index.php jetzt:

<?php 
if(have_posts()) :
    while(have_posts())  : the_post();?>
<?php the_content('<br />→ weiterlesen …'); ?>
<?php endwhile; ?>
<div><?php the_posts_pagination( array(
    'mid_size' => 4,
    'prev_text' => __( '←', 'neuere Beiträge' ),
    'next_text' => __( '→', 'ältere Beiträge' ),
) ); ?></div><p>&nbsp;</p>
        <?php else :?>
<h3><?php _e('404 Error: Not Found', ''); ?></h3>
<?php endif; ?>
<?php wp_reset_postdata();?>

und für Interessierte:

„Nearly everyone has pagination issues with custom queries (including get_posts()) on templates on their initial foray into subject. This is compounded with a lot of bad examples on the internets. The issues introduced are not readily apparent.

The only pagination function that really works with custom queries is paginate_links(). All other functions assume you want to paginate the requested page and not the queried posts.

Even with paginate_links(), you still run into trouble by trying to use the “paged” query var. It rightly belongs to the main query and not your custom query. Trying to usurp it for your own means does not work very well. IMO you’re better off introducing your own pagination query var to avoid any confusion and cross talk between queries.

Better yet, don’t do custom queries on a template if those results are going to be the only queried content on the page. Instead, alter the main query through the “pre_get_posts” action so it returns the results your custom query would have returned. Then there’s no need for another query, and WP handles pagination for you. Then most pagination functions will work as intended and you don’t have to use paginate_links(), though it would still work.“

  • Diese Antwort wurde geändert vor 7 Stunden, 25 Minuten von treibstoff. Grund: Korrektur
  • Diese Antwort wurde geändert vor 29 Minuten von Hans-Gerd Gerhards.
[ad_2]

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer