La paginazione di WordPress non funziona sul template search.php
2 gen 2018, 09:32:18
Visualizzazioni: 359
Voti: 1
Ho un piccolo problema, la paginazione non funziona su search.php
.
Eseguo la query dei risultati con wp_Query
utilizzando il codice qui sotto,
riesco a vedere i link delle pagine ma quando li clicco ottengo un errore 404.
Mi è stato detto che ciò accade perché eseguo la query sulla pagina search.php
.
Il codice che sto usando è preso dal codice di paginazione wp, è una query lunga ma le parti importanti sono queste due righe:
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
e
'paged' => $paged,
query completa
if (isset($_GET['reg-prop-search-form'])){
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
$args = array(
'posts_per_page' => 9,
's' => $_GET['s'],
'post_type' => 'property',
'paged' => $paged,
'meta_query' => array(
'relation ' => 'AND',
array(
'key' => 'prop_area',
'value' => $area,
'compare' => $area_val,
),
array(
'key' => 'prop_city',
'value' => $city,
'compare' => $city_val,
),
array(
'key' => 'prop_type',
'value' => $type,
'compare' => $type_val,
),
array(
'key' => 'prop_rooms',
'value' => array( $fromrooms, $torooms ),
'type' => 'numeric',
'compare' => 'BETWEEN',
),
array(
'key' => 'prop_store',
'value' => array( $fromfloor, $tofloor ),
'type' => 'numeric',
'compare' => 'BETWEEN',
),
array(
'key' => 'prop_price',
'value' => array( $min, $max ),
'type' => 'numeric',
'compare' => $equal,
),
),
);
};
questo è il loop
$the_query = new WP_Query( $args );
.....
<?php if( $the_query->have_posts() ): ?>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php get_template_part( 'content', 'search' ); ?>
<?php endwhile; ?>
<div id="pagination">
<?php
$big = 999999999;
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $the_query->max_num_pages
) );
?>
<?php echo paginate_links( $args ); ?>
</div>
<?php else : ?>
<?php get_template_part( 'no-results', 'search' ); ?>
<?php endif; ?>
<?php wp_reset_query(); // Ripristina i dati globali del post sovrascritti da the_post(). ?>
</div>

DavSev
143
Commenti
Domande correlate
1
risposte
0
risposte