web-dev-qa-db-fra.com

Faire une boucle personnalisée dans single.php avec la pagination

Je veux créer une boucle personnalisée dans un fichier single-ediciones.php et j'ai besoin d'une pagination (en fait, AJAX pagination).

J'ai besoin de ma boucle normale parce que je dois montrer certaines données du CPT "Ediciones" et je veux une boucle personnalisée pour montrer certains CPT liés au CPT actuel via ID.

En fait, je ne sais pas s'il est possible d'avoir une boucle normale et à l'intérieur d'une boucle personnalisée avec une pagination.

Pour le moment, je peux obtenir le travail "ALL_THE_URL/page/2", mais sans pagination. Même en utilisant new WP_Query et en ajoutant la get_next_posts_link( 'Older Entries', $the_query->max_num_pages );.

<?php if ( have_posts() ) :
    while( have_posts() ) : the_post();
        $edicion_ID = get_the_ID();
        $edicion_titulo = get_the_title();
        $edicion_titulo = strtolower($edicion_titulo); ?>
        <div class="container ediciones-detalle-contenedor">
            <div class="row">
                <div class="col-md-4 col-md-Push-8 col-sm-4 col-sm-Push-8">
                    <h3><?php echo $edicion_titulo; ?></h3>
               </div>

               <div class="col-md-8 col-md-pull-4 col-sm-8 col-sm-pull-4">
                   <div class="row">
                        <?php 
                        $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
                        $args = array(
                            'numberposts'   => 4,
                            'post_type'     => array('temas', 'perfiles', 'udem-mundo', 'deportes', 'cultura'),
                            'meta_query'    => array(
                                'relation'      => 'AND',
                                array(
                                    'key'       => 'contenido_relacionado_impreso',
                                    'value'     => 'si',
                                    'compare'   => '=',
                                ),
                                array(
                                    'key'       => 'contenido_relacionado_edicion',
                                    'value'     => $edicion_ID,
                                    'compare'   => '=',
                                ),
                            ),
                            'paged'         => $paged,
                        );

                        $posts_relacionados_edicion = get_posts($args);
                        if ( !empty($posts_relacionados_edicion) ) :
                            foreach ($posts_relacionados_edicion as $post) : setup_postdata( $post ); ?>
                                <div class="col-md-6 col-sm-6">
                                    <a href="<?php the_permalink(); ?>" class="blog-titulo-chico"><h3><?php the_title(); ?></h3></a>
                                </div>
                            <?php endforeach;
                            wp_reset_postdata();
                        endif; ?>
                    </div>
                </div>
            </div>
        </div>
    <?php endwhile;
endif; ?>
1
Jabel Márquez

À ce que je sache, la fonction get_posts() ne peut pas être utilisée pour des résultats paginés. Il est destiné à obtenir un tableau de publications et définit no_found_rows à true par défaut à partir de WP 3.1 . La pagination ne fonctionnera pas si vous ne la définissez pas à false ou mieux, utilisez WP_Query.

numberposts est également un argument invalide.

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

$args = array(
    'posts_per_page'   => 4,
    'post_type'       => array('temas', 'perfiles', 'udem-mundo', 'deportes', 'cultura'),
    'meta_query'    => array(
        'relation'      => 'AND',
         array(
            'key'       => 'contenido_relacionado_impreso',
            'value'     => 'si',
            'compare'   => '=',
         ),
         array(
             'key'       => 'contenido_relacionado_edicion',
             'value'     => $edicion_ID,
             'compare'   => '=',
         ),
     ),
     'paged'         => $paged,
 );

 $posts_relacionados_edicion = new WP_Query( $args );

 if ( $posts_relacionados_edicion->have_posts() ) :

     while( $posts_relacionados_edicion->have_posts() ) :

         $posts_relacionados_edicion->the_post(); ?>

         <div class="col-md-6 col-sm-6">
                 <a href="<?php the_permalink(); ?>" class="blog-titulo-chico"><h3><?php the_title(); ?></h3></a>
              </div>

      <?php endwhile;

      echo paginate_links( array(
          'current' => $paged,
          'total' => $posts_relacionados_edicion->max_num_pages,
          'format' => '?page=%#%'
      ) );

       wp_reset_postdata();

  else : ?>

     <?php _e( 'No found posts.', 'textdomain' ); ?>

  <?php endif; ?>

Le code ci-dessus génère des liens de pagination au format example.com/{post-name}/{page-number} et semble fonctionner.

Le code suivant génère des liens de pagination au format example.com/{post-name}/page/{page-number}, mais ils sont redirigés vers example.com/{post-name}/ et empêchent la pagination de fonctionner. Si vous souhaitez utiliser des URL telles que example.com/{post-name}/page/{page-number}:

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

$args = array(
    'posts_per_page'   => 4,
    'post_type'       => array('temas', 'perfiles', 'udem-mundo', 'deportes', 'cultura'),
    'meta_query'    => array(
        'relation'      => 'AND',
         array(
            'key'       => 'contenido_relacionado_impreso',
            'value'     => 'si',
            'compare'   => '=',
         ),
         array(
             'key'       => 'contenido_relacionado_edicion',
             'value'     => $edicion_ID,
             'compare'   => '=',
         ),
     ),
     'paged'         => $paged,
 );

 $posts_relacionados_edicion = new WP_Query( $args );

 if ( $posts_relacionados_edicion->have_posts() ) :

     while( $posts_relacionados_edicion->have_posts() ) :

         $posts_relacionados_edicion->the_post(); ?>

         <div class="col-md-6 col-sm-6">
                 <a href="<?php the_permalink(); ?>" class="blog-titulo-chico"><h3><?php the_title(); ?></h3></a>
              </div>

      <?php endwhile;

      echo paginate_links( array(
          'current' => $paged,
          'total' => $posts_relacionados_edicion->max_num_pages
      ) );

       wp_reset_postdata();

  else : ?>

     <?php _e( 'No found posts.', 'textdomain' ); ?>

  <?php endif; ?>

Vous pouvez également créer votre propre point final de pagination:

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

$args = array(
    'posts_per_page'   => 4,
    'post_type'       => array('post'),
    'paged'         => $paged,
 );

 $posts_relacionados_edicion = new WP_Query( $args );

 if ( $posts_relacionados_edicion->have_posts() ) :

     while( $posts_relacionados_edicion->have_posts() ) :

         $posts_relacionados_edicion->the_post(); ?>

         <div class="col-md-6 col-sm-6">
                 <a href="<?php the_permalink(); ?>" class="blog-titulo-chico"><h3><?php the_title(); ?></h3></a>
              </div>

      <?php endwhile;

      echo paginate_links( array(
          'current' => $paged,
          'total' =>  $posts_relacionados_edicion->max_num_pages,
          'format' => '?my_page=%#%'
      ) );

       wp_reset_postdata();

  else : ?>

     <?php _e( 'No found posts.', 'textdomain' ); ?>

  <?php endif; ?>

Combiné avec la balise rewrite:

add_action('init', function() {
    add_rewrite_tag( 'my_page', '([0-9]+)' );
} );
4
cybmeta