Pagination zeigt nicht alle Beiträge auf Index.php

[ad_1]

Hallo,
Fragen zur Programmierung solltest du eher auf stellen. Wir klären hier im Forum in erster Linie Fragen zur Anwendung von WordPress.

Außerdem noch ein allgemeiner Hinweis: Ohne Angabe der URL und des Berichts können wir dir nicht helfen. Lies bitte auch noch mal: Bevor du ein neues Thema (Thread) erstellst.

Viele Grüße
Hans-Gerd

Der Code ist auch etwas wirr.

Mit

if ( get_query_var( 'paged' ) ) { $paged = get_query_var( 'paged' ); }
elseif ( get_query_var( 'page' ) ) { $paged = get_query_var( 'page' ); }
else { $paged = 1; }

setzt du eine Variable $paged, die du anschließend mit

$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;

wieder überschreibst?

Dann startest du mit $the_query = new WP_Query( $args ); eine neue Datenbankabfrage der Posts … die du aber nie verwendest? Statt dessen verwendest du $myposts = get_posts( $args ); mit den gleichen Argumenten.

Bei den Argumenten verwendest du numberposts – warum möchtest du hier die maximale Anzahl von Beiträgen vorgeben, wenn du anschließend deine Anzahl der Beiträge in Einstellungen > Lesen kontrollierst?

Und welche Übersetzung erwartest du bei 'prev_text' => __( '←', 'textdomain' ),?

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 <strong>“pre_get_posts”</strong> 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.

[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